Excel 选择列中具有值的所有单元格,并使用VBA将文本转换为列

Excel 选择列中具有值的所有单元格,并使用VBA将文本转换为列,excel,vba,Excel,Vba,我的任务是: 1) 选择行中包含值的所有单元格(完成)。 2) 文本到列-示例值2017.01.01 Sub selectAndToColumns() Dim LR As Long LR = Range("A" & Rows.Count).End(xlUp).Row Range("A1:A" & LR).SpecialCells(xlCellTypeConstants, 23).Select '2nd step Selection.TextToCo

我的任务是:

1) 选择行中包含值的所有单元格(完成)。 2) 文本到列-示例值2017.01.01

Sub selectAndToColumns()
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("A1:A" & LR).SpecialCells(xlCellTypeConstants, 23).Select
'2nd step
    Selection.TextToColumns Destination:=Range(A1), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :=".", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1))
End Sub

第一步可以,但如何使用我的选择而不是第二步中的范围(A1)?我能做一个变量吗?怎么做

我不确定是否正确理解了您的问题,但如果您只想选择列A并将其粘贴为值,我会使用:

Columns("A:A").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
     :=False, Transpose:=False
Application.CutCopyMode = False

我不太确定您的要求,但这将在不选择任何内容的情况下执行TextToColumns(您仍然可以将目的地更改为您希望放置结果数据的任何位置,您可以通过指定范围或甚至使用存储范围的变量来执行此操作):

更新

如果您想用一个变量而不是范围(“A1”)来替换目标,那么下面类似的方法将起作用:

Sub selectAndToColumns()
    Dim DestinationRange As String
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    DestinationRange = "D1"
    MsgBox DestinationRange
    Range("A1:A" & LR).SpecialCells(xlCellTypeConstants, 23).TextToColumns Destination:=Range(DestinationRange), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :=".", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1))
End Sub

你对我的选择是什么意思?我理解你的问题。。。。TextToColumns将用于整个列,而不是该列中的某一组单元格。我同意不需要在步骤1中-我只需要选择整个列,但如何选择?即使在我的宏中也选择“工作”,但我需要在之后将文本添加到列中。无论如何,谢谢你的回答。
Sub selectAndToColumns()
    Dim DestinationRange As String
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    DestinationRange = "D1"
    MsgBox DestinationRange
    Range("A1:A" & LR).SpecialCells(xlCellTypeConstants, 23).TextToColumns Destination:=Range(DestinationRange), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :=".", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1))
End Sub