所以我有一个列通过不同的模块列出,有些重复5行,有些为20。我试图通过VBA合并这些,但是我的代码有问题。这是我正在使用的Sub,我已经评论了在我逐行调试之后错误发生的地方。非常感谢有关此错误的任何输入!
谢谢你们!
注意:
我从7开始,因为那是模块记录开始的行。
Sub ReMergeECURowsMPNT()
Dim wsMPNT As Worksheet
Set wsMPNT = Worksheets("Module Part Number Tracker")
Dim lrowMPNT As Long
lrowMPNT = CRC.LastRowInMPNT
Dim i As Long
Dim j As Long
Dim sameRows As Boolean
sameRows = True
For i = 7 To lrowMPNT
If StrComp(Cells(i, 3), Cells(i + 1, 3), vbTextCompare) Then
sameRows = False
End If
If sameRows = False Then
With wsMPNT
.Range(Cells(i, 3), Cells(i + 1, 3)).Merge '''Application defined error on this line
End With
End If
sameRows = True
Next i
End Sub
答案 0 :(得分:0)
除非DisplayAlerts
设置为False
,否则通常应该得到类似的提示“选择包含多个数据值。合并到一个单元格中将仅保留左上角的数据。 “,如果单击”取消“而不是”确定“,VBA将抛出”应用程序定义或对象定义的错误“< / em>的
另外,请检查第lrowMPNT = CRC.LastRowInMPNT
行。如果您正在寻找合并“C列”,您可以尝试这样简单的事情:
Sub ReMergeECURowsMPNT()
Application.DisplayAlerts = False
Dim wsMPNT As Worksheet, lrowMPNT As Long, i As Long, j As Long
Set wsMPNT = Worksheets("Module Part Number Tracker")
wsMPNT.Select
lrowMPNT = wsMPNT.UsedRange.Row - 1 + wsMPNT.UsedRange.Rows.Count
For i = 7 To lrowMPNT
If Cells(i, 3) = Cells(i + 1, 3) Then
j = j + 1
ElseIf j > 0 Then
Range(Cells(i - j, 3), Cells(i, 3)).Merge
j = 0
End If
Next i
Application.DisplayAlerts = True
End Sub