Reputation: 51
I'm trying to do a macro that can find a column by the name of the header , then to select entire column and run the "text to column" command.
I've recorded the macro based on the current position of the columns:
Sub TTC()
Columns("A:A").Select 'text to column and format it as TEXT
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:=" ", FieldInfo:=Array(1, 2), TrailingMinusNumbers:=True
Columns("D:D").Select 'text to column and format it as GENERAL
Selection.TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:=" ", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End Sub
I also have this macro to find the columns number:
Set txt = Sheet1.Cells(1, 1).EntireRow.Find(What:="Text", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
CT = txt.Column
Set val = Sheet1.Cells(1, 1).EntireRow.Find(What:="Value", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
CV = val.Column
How can I combine these two macros?
Upvotes: 0
Views: 8113
Reputation: 57683
val
as variable name because it is a reserved word.Select
or Selection
this is a bad practice.Option Explicit
and declare all your variables.This should give you an idea how to combine such commands:
Option Explicit
Public Sub TTC()
'text to column and format it as TEXT
Dim RangeTxt As Range
Set RangeTxt = Sheet1.Cells(1, 1).EntireRow.Find(What:="Text", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
Columns(RangeTxt.Column).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:=" ", FieldInfo:=Array(1, 2), TrailingMinusNumbers:=True
'text to column and format it as GENERAL
Dim RangeVal As Range
Set RangeVal = Sheet1.Cells(1, 1).EntireRow.Find(What:="Value", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
Columns(RangeVal.Column).TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, OtherChar _
:=" ", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End Sub
Upvotes: 1