【问题标题】:Excel VBA Loop on columns列上的 Excel VBA 循环
【发布时间】:2020-10-29 22:19:56
【问题描述】:

当我们要在行中循环时,我们可以使用如下代码:

i = 1
Do
   Range("E" & i & ":D" & i).Select
   i = i + 1
Loop Until i > 10

但是如果我们想在列上做一个循环呢?

我们可以使用与上面相同的方法吗?

而 Excel 中的列是复杂的,例如 A、B、C、...、Y、Z、AA、AB、AC、...等。 从“Z”到“AA”的循环之间会出现问题。

我们如何将字母列从“A”循环到“Z”,然后继续循环到“AA”、“AB”等

有什么可以帮忙的吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    是的,我们以Select为例

    示例代码:Columns("A").select

    如何循环遍历列:

    方法一:(可以用index代替Excel地址)

    For i = 1 to 100
        Columns(i).Select
    next i
    

    方法二:(使用地址)

    For i = 1 To 100
     Columns(Columns(i).Address).Select
    Next i
    

    编辑: 剥离 OP 的列

    columnString = Replace(Split(Columns(27).Address, ":")(0), "$", "")
    

    例如想得到第27列-->AA,这样就可以得到

    【讨论】:

    • + 1 用于同时提供这两种方法... :) 虽然当你有方法 1 时,第二个变得无用。
    • 你知道我们如何以循环为字符串获取列索引吗?像“A”、“B”、...、“AA”、“AB”、...等等,所以我可以像这样将它应用到我的循环中:Range(colIndex & i & ":" & colIndex & i).Select "colIndex" 是一个变量在excel中循环列索引
    • @Larry 最好问问他为什么需要列名 :) 我很好奇
    • @Rebel 通常不需要使用列字母;这正是 Excel UI 的设计方式,但在底层,列的索引与任何其他集合一样...使用数字。所以除非真的有必要使用数字。
    【解决方案2】:

    另一种尝试方法。 当您将初始列设置为 Range 对象时,还可以替换 select。在性能方面它会有所帮助。

    Dim rng as Range
    
    Set rng = WorkSheets(1).Range("A1") '-- you may change the sheet name according to yours.
    
    '-- here is your loop
    i = 1
    Do
       '-- do something: e.g. show the address of the column that you are currently in
       Msgbox rng.offset(0,i).Address 
       i = i + 1
    Loop Until i > 10
    

    ** 使用列号获取列名的两种方法**

    • 拆分()

    代码

    colName = Split(Range.Offset(0,i).Address, "$")(1)
    
    • 字符串操作:

    代码

    Function myColName(colNum as Long) as String
        myColName = Left(Range(0, colNum).Address(False, False), _ 
        1 - (colNum > 10)) 
    End Function 
    

    【讨论】:

    • 你知道我们如何以循环为字符串获取列索引吗?像“A”、“B”、...、“AA”、“AB”、...等,所以我可以像这样将它应用到我的循环中:Range(colIndex & i & ":" & colIndex & i).Select "colIndex" 是一个变量在excel中循环列索引
    • @Rebel 这是可能的。但是出于好奇,既然有很多行之有效的方法来遍历列,为什么要获取String column name
    • @Rebel 我已经使用了两种不同的方法来查找列名。请与我们分享您为什么需要它:D
    【解决方案3】:

    如果您想坚持使用相同类型的循环,那么这将起作用:

    Option Explicit
    
    Sub selectColumns()
    
    Dim topSelection As Integer
    Dim endSelection As Integer
    topSelection = 2
    endSelection = 10
    
    Dim columnSelected As Integer
    columnSelected = 1
    Do
       With Excel.ThisWorkbook.ActiveSheet
            .Range(.Cells(columnSelected, columnSelected), .Cells(endSelection, columnSelected)).Select
       End With
       columnSelected = columnSelected + 1
    Loop Until columnSelected > 10
    
    End Sub
    

    编辑

    如果实际上您只想遍历电子表格区域中的每个单元格,请使用以下内容:

    Sub loopThroughCells()
    
    '=============
    'this is the starting point
    Dim rwMin As Integer
    Dim colMin As Integer
    rwMin = 2
    colMin = 2
    '=============
    
    '=============
    'this is the ending point
    Dim rwMax As Integer
    Dim colMax As Integer
    rwMax = 10
    colMax = 5
    '=============
    
    '=============
    'iterator
    Dim rwIndex As Integer
    Dim colIndex As Integer
    '=============
    
    For rwIndex = rwMin To rwMax
            For colIndex = colMin To colMax
                Cells(rwIndex, colIndex).Select
            Next colIndex
    Next rwIndex
    
    End Sub
    

    【讨论】:

    • 你知道我们如何以循环为字符串获取列索引吗?像“A”、“B”、...、“AA”、“AB”、...等,所以我可以像这样将它应用到我的循环中:Range(colIndex & i & ":" & colIndex & i).Select "colIndex" 是一个变量在excel中循环列索引
    • @Rebel 与 .Range(.Cells(i, colIndex), .Cells(i, colIndex)).Select 相同 ....这就是我在建议的答案中提出的内容,只是名称更具描述性。但是在您的评论中,您提到了Range(colIndex & i & ":" & colIndex & i).Select ....不需要冒号后的 repeated 部分:Range(colIndex & i).Select ....实际上相当于.cells(i,colIndex)
    【解决方案4】:

    只需使用 Cells 函数并循环遍历列。 单元格(行、列)

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      相关资源
      最近更新 更多