我試圖通過列「M」中的數字更改將合併列「D」到「L」的代碼。合併範圍隨着列M中的變化而改變
我有以下代碼,但它所做的全部操作是從底部到第2行合併每一行,而不管列「M」中的值如何。
我失蹤了什麼?
Sub Merge_Upon_Change()
'Purpose: Merges cells between columns "D" and "L" when column "M" changes
Dim r As Long, i As Long
Application.DisplayAlerts = False 'Turn off windows warning popup
r = Cells(Rows.Count, "D").End(xlUp).row ' find last cell in Column D
For i = r To 2 Step -1
If Cells(i, 13).Value <> Cells(i + 13, 13).Value Then 'upon change in column M = 13
Range("D" & i & ":L" & i).Merge 'then merge column "D" through "L"
End If
Next i
Application.DisplayAlerts = True ''Turn on Windows warning popup
End Sub
Maybe'Cells(i + 13,...'should be'Cells(i + 1,...'? –
將代碼更改爲'如果Cells(i,13).Value <> Cells(i + 1,13).Value Then'做了這個訣竅。謝謝!! – XLmatters