我正在尝试正确的代码,当数字在“M”列中更改时,将“D”列合并为“L”。
我有以下代码,但它所做的就是从下到上合并每一行到第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
答案 0 :(得分:1)
实际上你已经提出了这个问题,但假装这个问题没有得到答复我将发布这个答案,以便将来搜索这个问题。
当您将等式写为 M i &lt;&gt; M i + 13 然后它只是找到每个等式 True (因为可能 i + 13 th 行不等于到你的我行),因此它将从底部到第二行的所有内容合并为 For循环直到 2 强>
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 + 1, 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