2024年4月24日发(作者:)

VBA合并单元格(MergecellsInVBA)

VBA合并单元格(Merge cells In VBA)

━━━━━━━━━━━━━━━━━━━━━━━━━

'单元格合并

Sub h1()

Range("g1:h3").Merge '

End Sub

'合并区域的返回信息

Sub h2()

Range("e1") = Range("b3").s '返回单元格

所在的合并单元格区域

End Sub

'判断是否含合并单元格

Sub h3()

'MsgBox Range( "b2 ").MergeCells

' MsgBox Range( "A1:D7 ").MergeCells

Range("e2") = IsNull(Range("a1:d7").MergeCells)

Range("e3") = IsNull(Range("a9:d72").MergeCells)

End Sub

'综合示例:合并H列相同单元格

Sub h4()

Dim x As Integer

Dim rg As Range

Set rg = Range("h1")

yAlerts = False

For x = 1 To 13

If Range("h" & x + 1) = Range("h" & x) Then

Set rg = Union(rg, Range("h" & x + 1))

Else

Set rg = Range("h" & x + 1)

End If

Next x

yAlerts = True

End Sub

━━━━━━━━━━━━━━━━━━━━━━━━━