vba 如何查找excel中的单元格是否合并?如果单元格mrged如何读取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14599841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get find if a cell in excel is merged? If the cell mrged how to read the value?
提问by Ashikur Rahman
How do I detect if a cell in excel is merged?
如何检测excel中的单元格是否合并?
If the cell is merged how do I read the value?
如果单元格被合并,我如何读取值?
回答by Vinny Roe
I don't think there's any formula to tell you if a cell is merged or not. You can write your own public vbafunction, put it in a code Module, and then use that on your sheet:
我认为没有任何公式可以告诉您单元格是否合并。您可以编写自己的公共vba函数,将其放入代码模块中,然后在您的工作表上使用它:
Function IsMerged(rCell As Range) As Boolean
' Returns true if referenced cell is Merged
IsMerged = rCell.MergeCells
End Function
Then as an Excel formula to test cell A1
:
然后作为 Excel 公式来测试单元格A1
:
=IsMerged(A1)
回答by Wilson
Here's how to read the cell's value in VBA, regardless if it is merged or not.
以下是如何在 VBA 中读取单元格的值,无论是否合并。
C.MergeArea.Cells(1, 1).Value
C.MergeArea.Cells(1, 1).Value
where C
is the cell you want to look at. The way this works is that the MergeArea is either exactly the same as the cell itself if the cell is not merged; otherwise the MergeArea is the range of cells that have been merged. And when the cells are merged, the value is kept in the topleftmost cell.
C
您要查看的单元格在哪里。其工作方式是,如果单元格未合并,则 MergeArea 与单元格本身完全相同;否则 MergeArea 是已合并的单元格范围。合并单元格时,该值保留在最左上角的单元格中。
回答by ZAT
Hurray! Figured out a way to check whether a cell is merged and return that cell's value:
欢呼!想出一种检查单元格是否合并并返回该单元格值的方法:
Sub checkCellMerged1()
'With ThisWorkbook.ActiveSheet
Set ma = ActiveCell.MergeArea
On Error GoTo errHand
If ma = ActiveCell Then MsgBox ActiveCell.Address & " is not merged"
GoTo final
errHand:
If Err.Number = 13 Then MsgBox "Merged Address = " & ma.Address _
& vbCrLf & "Value = " & ma(1).Value
final:
On Error GoTo 0
'End With
End Sub
回答by Chris Puckett
If B and C are always populated there is a simple non-VBA method to determine if a cell is merged. Simply do COUNTA(B2,C2) and check the total. If B2 is merged with C2, the total will be 1, if it's not the count will be 2.
如果始终填充 B 和 C,则有一个简单的非 VBA 方法来确定单元格是否合并。只需执行 COUNTA(B2,C2) 并检查总数。如果 B2 与 C2 合并,则总数为 1,否则计数为 2。