是否可以在vba中循环合并的单元格。
B4:B40
答案 0 :(得分:8)
以上答案看起来让你排序。
如果您不知道合并单元格的位置,则可以使用以下例程快速检测它们。
当我构建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
答案 1 :(得分: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
答案 2 :(得分:2)
稍微紧缩,类似的想法:
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