简体   繁体   中英

VBA: Text to column for a specific columns

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?

  1. You should not use val as variable name because it is a reserved word.
  2. You should avoid using Select or Selection this is a bad practice.
  3. You should use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM