在VBA中循环合并单元格

是否有可能在vba中循环合并的单元格。

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

上面的答案看起来有你sorting。

如果您不知道合并的单元格在哪里,则可以使用以下例程来快速检测它们。

当我build立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 

这是你的问题的第一个刺:

 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 

只是一个更紧一点,类似的想法:

 Option Explicit Sub ListValues() Dim i As Long For i = 4 To 40 Step 6 Debug.Print Range("B" & i).Value Next i End Sub