简体   繁体   English

Excel VBA 获取合并单元格的内容

[英]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.在 VBA 中,我得到第 8 行 AV、AU、AW 的内容,就像这样。

.Cells(row_level - 1, col_comment)

Row 7 is a merged cell.第 7 行是合并单元格。 I would like to get the contents of AV if the column is AV, AU, or AW.如果列是 AV、AU 或 AW,我想获取 AV 的内容。 The tricky part is that there aren't always three merged columns.棘手的部分是并不总是三个合并的列。 There might be anywhere from 2 to 7.可能有 2 到 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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