我在使用带有合并单元格的偏移的正确语法时遇到问题。例如,在A列中,第2行到第5行(A2:A5)是合并的单元格。我想使用偏移来获得列的范围(B2:B5),包括合并的单元格值(A2:B5)。从那里,我想将该偏移范围用作聚类柱图的值。我现在的代码只获得第一个单元格值(B2)但不是(A2:B5)。任何帮助,将不胜感激。
Dim ws As Worksheet: Set ws = Sheets("Trend")
Dim LastRow As Long, partnum As String, findpart As Range
Dim lastrowmerge As Long, FirstRow As Long
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
partnum = TextBox1.Value
' Searches for number in column A including merged cells
If Not Application.IsNA(Application.Match(partnum, Range("A:A"), 0)) Then
Set findpart = Cells(Application.Match(partnum, Range("A:A"), 0), 1)
' If number found , get range values to the right of number found in column A
Dim xVals As Long
Set mymergedrange = findpart.MergeArea
' FirstRow = mymergedrange.Row
' lastrowmerge = mymergedrange.Row + mymergedrange.Rows.Count - 1
xVals = mymergedrange.Offset(0, 1) ' (B2 not B2:B5)
答案 0 :(得分:-1)
请将单元格的合并区域仅指为左上角的单元格。
例如,如果合并了A2:A5
,请参考它,就好像它只是A2
。
同样适用于抵消,但是合并的单元格可能会以多种方式引起混淆,并且通常应该使用包含值的单元格来避免。合并通常仅用于标签/标题。
如果必须使用合并单元格,您可以使用以下属性确定合并区域的尺寸:
Sub mergeTest()
Dim r As Range
Set r = ActiveSheet.Range("A2")
With r
If Not r.MergeCells Then
MsgBox .Address & " is not part of merged cells."
Exit Sub
End If
'cell is merged
MsgBox .Address & " is part of a merged set of " & _
.MergeArea.Cells.Count & " cells." & vbLf & _
"It is " & .MergeArea.Columns.Count & " columns tall by " & _
.MergeArea.Columns.Count & " rows wide." & vbLf & _
"The address of the merged range is: " & .MergeArea.Address
End With
End Sub
Stack Overflow:Detect merged cells in VBA Excel with MergeArea
Stack Overflow:Excel VBA Range Merge Cells and offset
PowerSheets:Excel VBA Merge Cells (注意 - 大量广告)
MSDN:MergeArea Property