简体   繁体   English

遍历列

[英]Iterate over columns

I want to create a vba macro that transforms text to columns, but this command is only capable of doing column by column.我想创建一个将文本转换为列的 vba 宏,但此命令只能逐列执行。

    Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 1), TrailingMinusNumbers:=True

This code above works whell for one column, but i would like to do the command for the columns F to BL.上面的代码适用于一列,但我想对列 F 到 BL 执行命令。

How can i do a For Loop to iterate over all the columns between F and BL?如何执行 For 循环来遍历 F 和 BL 之间的所有列?

Some sort of For Columns in Range Do ? For Columns in Range Do吗?

Something like this should give you an idea of what you can do.这样的事情应该让你知道你能做什么。

Note that you don't need to select.请注意,您不需要 select。 And most of those option are probably set to their default value, making the code unecessary verbose.而且这些选项中的大多数可能都设置为它们的默认值,从而使代码变得不必要的冗长。 The macro recorder is nice but you might want to rework the result.宏记录器很好,但您可能想重新处理结果。

Sub quick_and_dirty()
    Dim cell As Range
    
    For Each cell In Range("F1:G1")
        cell.EntireColumn.TextToColumns Destination:=cell, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
    Next cell
End Sub

You are going to run into an issue with regard to overwriting existing data, so the route you want to follow is dependent on how you want to address that.您将遇到覆盖现有数据的问题,因此您要遵循的路线取决于您要如何解决该问题。

If you use AugustinLopez' method above, you'd want to make a handful of adjustments either way.如果您使用上述 AugustinLopez 的方法,则无论哪种方式都需要进行一些调整。

If you want to copy each row to a new sheet and then expand it, just add a "cell.EntireColumn copy xxx" step before the TextToColumns step and copy it to a new range.如果要将每一行复制到新工作表然后展开它,只需在 TextToColumns 步骤之前添加“cell.EntireColumn copy xxx”步骤并将其复制到新范围即可。

If you want to add the necessary number of empty columns first, and presuming you have a variable number of commas through out the columns, it gets trickier and you'd need something like this (depending on your table size, some optimization for speed may be needed):如果您想首先添加必要数量的空列,并假设您在列中有可变数量的逗号,它会变得更加棘手,您需要这样的东西(取决于您的表大小,一些速度优化可能被需要):

Sub DummyCode()

Dim cell As Range
Dim ColumnCount As Integer
Dim RangeOfInterest As Range
Dim CellStart As Integer
Dim i As Integer
Dim j As Integer
Dim CommaCount As Integer


Set RangeOfInterest = Range("F1:BL1")


ColumnCount = RangeOfInterest.Columns.Count
CellStart = RangeOfInterest.Range("A1").Column

For i = CellStart + ColumnCount - 1 To CellStart Step -1 'you gotta go backwards to not mess up your count
    CommaCount = 0
    For Each cell In Intersect(Cells(1, i).EntireColumn, ActiveSheet.UsedRange)
        
    If InStr(1, cell.Value, ",") > 0 Then
        If CommaCount < (Len(cell.Value) - Len(Replace(cell.Value, ",", ""))) Then
            CommaCount = (Len(cell.Value) - Len(Replace(cell.Value, ",", "")))
            'credit for the above bit of cleverness goes to here:
            'http://www.ozgrid.com/forum/showthread.php?t=45651
            'via here
            'https://stackoverflow.com/questions/9260982/how-to-find-number-of-occurences-of-slash-from-a-strings
        End If
        
    End If
    
    For j = 1 To CommaCount
        Cells(1, i).EntireColumn.Insert
    Next j
    
    Cells(1, i).EntireColumn.TextToColumns Destination:=Cells(1, i), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
    
Next i

End Sub

good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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