Looping through Merged cells in VBA

后端 未结 3 1616
孤街浪徒
孤街浪徒 2020-12-05 21:27

Is it possible to loop through merged cells in vba.

  • I have 6 merged cells in the range B4:B40
  • I need the values in these 6 cells 6 iterat
相关标签:
3条回答
  • 2020-12-05 21:45

    The above answers look to have you sorted.

    If you don't know where the merged cells are then you can use the following routine to quickly detect them.

    When I built Mappit! I realised that when I developed merged cell reporting that merged cells were part of xlBlanks

    So you can use the code to detect merged cells immediately rather than loop through each cell testing for the MergedCells property being 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
    
    0 讨论(0)
  • 2020-12-05 21:55

    Just a little tighter, similar idea:

    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
    
    0 讨论(0)
  • 2020-12-05 21:58

    Here is a first stab to your issue:

    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
    
    0 讨论(0)
提交回复
热议问题