Excel VBA - If cells is part of merged cells pass the value?

Seb*_*arz 3 excel vba

I'm having some trouble to prepare macro which would help me to pass the value to another cell if the specified cell is a part of merged cells. 问题

As you can see, cells A1-A15 are merged, in B1 I've written =A1 in B2 I did =A2, so what I want to achieve is that whenever I assign somewhere cell which is part of merged cells(A1-A15) the 'test' value is passed so there is no difference if I write =A1 or =A15 or =A10

I would appreciate any help of advice.

Sha*_*ado 5

You can detect if a Cell is part of a Merged Cell using If Range("A1").MergeCells = True.

获取MergedAreausing 中的行数Range("A" & i).MergeArea.Rows.Count

下面的代码中有更多解释。

代码

Option Explicit

Sub CheckifMergedCell()

Dim MergeRows As Long, i As Long

i = 1
While i < 100 ' 100 is just for example , change it later according to your needs

    If Range("A" & i).MergeCells = True Then
        MergeRows = Range("A" & i).MergeArea.Rows.Count ' number of merged cells
    Else ' not merged >> single row
        MergeRows = 1
    End If

    Range("B" & i).Resize(MergeRows, 1).Value = Range("A" & i).Value

    i = i + MergeRows
Wend

End Sub
Run Code Online (Sandbox Code Playgroud)

  • @QHarr 我有,不幸的是,有些经理在 Excel 中使用它,因为它对他们来说看起来不错,他们不知道它给我们带来的无数头痛:) (2认同)