循环通过VBA中的合并单元格

135*_*355 8 excel vba excel-vba

是否可以在循环合并的单元格.

  • 我在该范围内有6个合并的单元格 B4:B40
  • 我只需要6个单元格中的6个迭代值.

bre*_*tdj 9

上面的答案看起来让你排序.

如果您不知道合并单元格的位置,则可以使用以下例程快速检测它们.

当我建立Mappit!我意识到,当我开发合并的单元格报告时,合并的单元格是其中的一部分xlBlanks

因此,您可以使用代码立即检测合并的单元格,而不是循环遍历每个单元格测试以确保MergedCells属性为true.

Sub DetectMerged()
Dim rng1 As Range
Dim rng2 As Range
On Error Resume Next
Set rng1 = Intersect(Cells.SpecialCells(xlFormulas), Cells.SpecialCells(xlBlanks))
Set rng2 = Intersect(Cells.SpecialCells(xlConstants), Cells.SpecialCells(xlBlanks))
On Error GoTo 0
If Not rng1 Is Nothing Then MsgBox "Merged formulae cells in " & rng1.Address(0, 0)
If Not rng2 Is Nothing Then MsgBox "Merged constant cells in " & rng2.Address(0, 0)
End Sub
Run Code Online (Sandbox Code Playgroud)


JMa*_*Max 5

以下是对您的问题的第一次尝试:

Option Explicit

Sub loopOverCells()
    Dim rCell As Range
    Dim i As Integer

    Set rCell = [B1]
    For i = 1 To 6
        Debug.Print rCell.Address
        Set rCell = rCell.Offset(1, 0)    ' Jump 1 row down to the next cell
    Next i
End Sub
Run Code Online (Sandbox Code Playgroud)