excel 如何查找单元格是否合并并读取值?

ttcibm8c  于 2023-02-14  发布在  其他
关注(0)|答案(6)|浏览(225)

如何检测单元格是否已合并?
如果单元格被合并了,我该如何读取值?

von4xj4u

von4xj4u1#

我认为没有任何公式可以告诉你一个单元格是否合并,你可以编写自己的公共vba函数,把它放在一个代码模块中,然后在你的工作表中使用它:

Function IsMerged(rCell As Range) As Boolean
' Returns true if referenced cell is Merged        
          IsMerged = rCell.MergeCells        
End Function

然后作为Excel公式测试单元格A1

=IsMerged(A1)
ghhaqwfi

ghhaqwfi2#

下面介绍了如何在VBA中读取单元格的值,无论它是否合并。
C.MergeArea.Cells(1, 1).Value
其中C是您要查看的单元格。其工作方式是,如果单元格未合并,则MergeArea与单元格本身完全相同;否则,MergeArea是已经合并的单元格的范围。并且当单元格被合并时,该值保留在最上面的单元格中。

enxuqcxy

enxuqcxy3#

万岁!找到了一种检查单元格是否合并并返回单元格值的方法:

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
weylhg0b

weylhg0b4#

一个常见的需求是只针对合并区域中的第一个单元格。这段代码就是这样做的。
If语句结果仅对第一个单元格为真;合并与否。

Sub RunOnlyForFirstCell()
    Dim c As Range
    Dim MergedCellsArea As Range
    
    For Each c In Selection.Cells
        Set MergedCellsArea = c.MergeArea
        If c.Address = MergedCellsArea(1, 1).Address Then
            '''run your sub
            Debug.Print c.Address; Selection.Cells.Count; MergedCellsArea(1, 1).Address
        End If
    Next c
 End Sub
oalqel3c

oalqel3c5#

下面的代码回答了这两个问题

Function GetValue(iRow As Integer, iCol As Integer) As String
    Dim rCell As Range
    Set rCell = oSheet.Cells(iRow, iCol)
        
    sText = ""
            
    If Not rCell.MergeCells Then
        sText = rCell.Value
    End If
        
    GetValue = sText
End Function
...
Set oSheet = Worksheets("Sheet1")

使用Excel,我写了一个小数独表,我使用VBA宏来测试一个单元格范围是否已经合并。

在下面的例子(上图)中,我使用下面的代码来检测空的数独单元格,并尝试解决未找到的数字。
如果合并单元格(Excel 3x3范围),则它包含找到的数字。

'**********************************************************************
'* HighlightFoundCells()
'*-------------------------------------------------
'* When a Sudoku Cell (3x3 Excel range) contains only 1 small digit,
'* it is displayed using bigger font in middle of merged 3x3 Excel range
'**********************************************************************

For n = 1 To 9
    For i = 1 To 9
        iCount = 0
        For j = 1 To 9
            If GetDigitValue(n, i, j) = n Then
                iCount = iCount + 1
                jLast = j
            End If
        Next j
        If iCount = 1 Then
            Call MergeCells(iFirstRow + 3 * (i - 1), iFirstCol + 3 * (jLast - 1))
            ActiveCell.FormulaR1C1 = CStr(n)
            Call HighlightCell(iLast, j)
        End If
    Next i
Next n

'**********************************************************************
'* GetDigitValue()
'*---------------------------------------------------------------------
'* Get digit value contained in merged 3x3 cell's range
'* that represent a found digit.
'**********************************************************************

Function GetDigitValue(n As Integer, iRow9 As Integer, iCol9 As Integer) As Integer
    row = iFirstRow + (iRow9 - 1) * 3 + (n - 1) \ 3
    col = iFirstCol + (iCol9 - 1) * 3 + (n - 1) Mod 3
    
    Dim rCell As Range
    Set rCell = oSheet.Cells(row, col)
    
    Dim c As Integer: c = 0
        
    If Not rCell.MergeCells Then
        c = rCell.Value
    End If
    
    GetDigitValue = CInt(c)
End Function

'**********************************************************************
'* HighlightCell()
'*---------------------------------------------------------------------
'* Change background of 3x3 group of cells that contains a found digit
'**********************************************************************

Sub HighlightCell(i As Integer, j As Integer)
    nRow = iFirstRow + (i - 1) * 3
    nCol = iFirstCol + (j - 1) * 3
    oSheet.Range(Cells(nRow, nCol), Cells(nRow + 2, nCol + 2)).Select
    
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub

合并单元格范围的Excel代码如下所示

Sub MergeCells(iRow As Integer, iCol As Integer)
    Range(oSheet.Cells(iRow, iCol), oSheet.Cells(iRow + 2, iCol + 2)).Select
    Selection.ClearContents
    'Range("X8").Select
    'ActiveCell.FormulaR1C1 = "5"
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
    End With
    Selection.Merge
    With Selection.Font
        .Name = "Arial"
        .Size = 48
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
End Sub
xqk2d5yq

xqk2d5yq6#

如果B和C总是被填充,则有一个简单的非VBA方法来确定单元格是否被合并。只需执行COUNTA(B2,C2)并检查总数。如果B2与C2合并,则总数将为1,否则计数将为2。

相关问题