[英]Detect merged cells in VBA Excel with MergeArea
我遇到了這個問題 - 我必須從excel表中檢測水平和垂直合並的單元格。 我必須存儲第一個細胞系,以及合並區域的長度。 我逐行遍歷表中兩個for-cycles。
如何使用MergeArea屬性檢測合並和非合並區域? 如果單元格未合並,則應該返回空范圍,但是,這樣:
“如果currentRange什么都沒有”
根本不工作。 有任何想法嗎? 非常感謝。
這有幾個有用的代碼。
將光標放在合並的單元格中,並在Immidiate Window中詢問這些問題:
activecell是一個合並的單元格嗎?
? Activecell.Mergecells
True
合並了多少個單元格?
? Activecell.MergeArea.Cells.Count
2
合並了多少列?
? Activecell.MergeArea.Columns.Count
2
合並了多少行?
? Activecell.MergeArea.Rows.Count
1
合並范圍地址是什么?
? activecell.MergeArea.Address
$F$2:$F$3
雖然使用@tbur所示的選定單元格很有用,但它也不是唯一可用的選項。
您可以像這樣使用Range() :
If Worksheets("Sheet1").Range("A1").MergeCells Then
Do something
Else
Do something else
End If
要么:
If Worksheets("Sheet1").Range("A1:C1").MergeCells Then
Do something
Else
Do something else
End If
或者,您可以使用Cells() :
If Worksheets("Sheet1").Cells(1, 1).MergeCells Then
Do something
Else
Do something else
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.