我的macros中的一行VBA代码在它自己的工作,但整个macros运行时失败
我在运行macros之前单独testing了代码的每个部分,所有部分都工作得很好。
在我的代码失败的是:
SQL.Range(Cells(2, 2), Cells(SQL.UsedRange.Rows.Count, 2)).Copy
错误消息如下所示:运行时错误“1004”应用程序定义或对象定义的错误
这是我的代码,直到错误的地步。 有什么可能会出错?
Sub Prep() Dim BO, HOC, Pol, Adv, Asg, Pay, SQL, Fml, Tbl As Worksheet Dim c As Integer Set BO = Sheets("BO") Set HOC = Sheets("HOC") Set Pol = Sheets("Policy") Set Adv = Sheets("Advisor") Set Asg = Sheets("Assignee") Set Pay = Sheets("Payer") Set SQL = Sheets("SQL") Application.ScreenUpdating = False BO.Range("L:L").Insert BO.Range("L2").Value = "=DATE(LEFT(K2,4),MID(K2,5,2),RIGHT(K2,2))" With BO.Range(Cells(2, 12), Cells(BO.UsedRange.Rows.Count, 12)) .FillDown .Copy End With With BO.Range(Cells(2, 11), Cells(BO.UsedRange.Rows.Count, 11)) .PasteSpecial xlPasteValues .NumberFormat = "mm/dd/yyyy" End With BO.Columns(12).EntireColumn.Delete BO.Range("M:M").Insert BO.Range("M2").Value = "=DATE(LEFT(L2,4),MID(L2,5,2),RIGHT(L2,2))" With BO.Range(Cells(2, 13), Cells(BO.UsedRange.Rows.Count, 13)) .FillDown .Copy End With With BO.Range(Cells(2, 12), Cells(BO.UsedRange.Rows.Count, 12)) .PasteSpecial xlPasteValues .NumberFormat = "mm/dd/yyyy" End With BO.Columns(13).EntireColumn.Delete BO.Range("N:N").Insert BO.Range("N2").Value = "=IFERROR(DATE(LEFT(M2,4),MID(M2,5,2),RIGHT(M2,2)),"""")" With BO.Range(Cells(2, 14), Cells(BO.UsedRange.Rows.Count, 14)) .FillDown .Copy End With With BO.Range(Cells(2, 13), Cells(BO.UsedRange.Rows.Count, 13)) .PasteSpecial xlPasteValues .NumberFormat = "mm/dd/yyyy" End With BO.Columns(14).EntireColumn.Delete SQL.UsedRange.RemoveDuplicates Columns:=Array(1, 2, 3, 4), _ Header:=xlYes Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Formulas" Set Fml = Sheets("Formulas") With Fml.Cells(1, 1) .Value = "=EOMONTH(TODAY(),-1)" .Copy .PasteSpecial xlPasteValues .NumberFormat = "mm/dd/yyyy" End With SQL.Range(Cells(2, 2), Cells(SQL.UsedRange.Rows.Count, 2)).Copy
您正在尝试使用其他工作表中的两个Range.Cells属性来定义Range对象 。
明确定义您的定义中的每个参考。
SQL.Range(SQL.Cells(2, 2), SQL.Cells(SQL.UsedRange.Rows.Count, 2)).Copy
或者更优选的方法,
with SQL .Range(.Cells(2, 2), .Cells(.UsedRange.Rows.Count, 2)).Copy end with