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
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
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")
'**********************************************************************
'* 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
6条答案
按热度按时间von4xj4u1#
我认为没有任何公式可以告诉你一个单元格是否合并,你可以编写自己的公共vba函数,把它放在一个代码模块中,然后在你的工作表中使用它:
然后作为Excel公式测试单元格
A1
:ghhaqwfi2#
下面介绍了如何在VBA中读取单元格的值,无论它是否合并。
C.MergeArea.Cells(1, 1).Value
其中
C
是您要查看的单元格。其工作方式是,如果单元格未合并,则MergeArea与单元格本身完全相同;否则,MergeArea是已经合并的单元格的范围。并且当单元格被合并时,该值保留在最上面的单元格中。enxuqcxy3#
万岁!找到了一种检查单元格是否合并并返回单元格值的方法:
weylhg0b4#
一个常见的需求是只针对合并区域中的第一个单元格。这段代码就是这样做的。
If语句结果仅对第一个单元格为真;合并与否。
oalqel3c5#
下面的代码回答了这两个问题
使用Excel,我写了一个小数独表,我使用VBA宏来测试一个单元格范围是否已经合并。
在下面的例子(上图)中,我使用下面的代码来检测空的数独单元格,并尝试解决未找到的数字。
如果合并单元格(Excel 3x3范围),则它包含找到的数字。
合并单元格范围的Excel代码如下所示
xqk2d5yq6#
如果B和C总是被填充,则有一个简单的非VBA方法来确定单元格是否被合并。只需执行COUNTA(B2,C2)并检查总数。如果B2与C2合并,则总数将为1,否则计数将为2。