![](/img/trans.png)
[英]Vba code to compare columns that have cells containing dates and paste specific text to another column
[英]VBA: Text to column for a specific 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
我也有這個宏來查找列號:
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
如何合並這兩個宏?
val
用作變量名,因為它是保留字。 Select
或“ Selection
這是一個不好的做法。 Option Explicit
並聲明所有變量。 這應該使您知道如何組合這樣的命令:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.