简体   繁体   中英

Excel VBA getting contents of merged cells

I have this sheet.

在此处输入图片说明

In VBA I get the contents of row 8 column AV, AU, AW, like this.

.Cells(row_level - 1, col_comment)

Row 7 is a merged cell. I would like to get the contents of AV if the column is AV, AU, or AW. The tricky part is that there aren't always three merged columns. There might be anywhere from 2 to 7.

try,

Sub test()
    Dim rngDB As Range, rng As Range
    Dim vR()
    Dim i As Long, n As Long, j As Integer

    Set rngDB = Range("au8", Range("au" & Rows.Count).End(xlUp))

    For Each rng In rngDB
        If rng.MergeCells Then
            If rng.Address = rng.MergeArea.Range("a1").Address Then
                n = n + 1
                ReDim Preserve vR(1 To 3, 1 To n)
                For j = 1 To 3
                    vR(j, n) = rng(1, j)
                Next j
            End If
        Else
            n = n + 1
            ReDim Preserve vR(1 To 3, 1 To n)
            For j = 1 To 3
                vR(j, n) = rng(1, j)
            Next j
        End If
    Next rng
    Sheets.Add
    Range("a1").Resize(n, 3) = WorksheetFunction.Transpose(vR)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM