Range.Clear上的VBA运行时错误1004

有很多关于这个错误的线程,但是无论我尝试什么,我都无法得到这个工作。 大多数人说,当你尝试在不活动的表单上调用一个方法时,就会发生这种情况,但是你不应该这样做。 错误是在第28行。谢谢。

Private Sub CommandButton1_Click() Dim x As Integer Dim boisePaste As Integer Dim jrgPaste As Integer Dim master As Integer Dim lastRow As Integer Dim bookCount As Integer bookCount = Application.Workbooks.Count For x = 1 To bookCount If Left(Application.Workbooks(x).Name, 14) = "ITEM_INVENTORY" Then boisePaste = x ElseIf Left(Application.Workbooks(x).Name, 6) = "report" Then jrgPaste = x ElseIf Left(Application.Workbooks(x).Name, 8) = "Portland" Then master = x End If next x 'Unhide sheets and delete Boise range' Application.ActiveWorkbook.Sheets("BoisePaste").Visible = True Sheets("JRGpaste").Visible = True lastRow = Sheets("BoisePaste").Cells(Rows.Count, "B").End(xlUp).Row Sheets("BoisePaste").Range(Cells(1,2), Cells(lastRow, 23)).Clear 'Open Boise file and copy range, paste in master' Application.Workbooks(boisePaste).Activate With ActiveSheet .Range(.Cells(1,1), .Cells((.Cells(Rows.Count, "A").End(xlUp).Row),22)).Copy End With Application.Workbooks(master).Sheets("BoisePaste").Range(B1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False 'Open JRG report and copy range, paste in master' Application.Workbooks(jrgPaste).Activate ActiveSheet.Cells.Copy Application.Workbooks(master).Sheets("JRGpaste").Range(A1).Paste Application.CutCopyMode = False 'Refresh pivot tables; hide sheets' Application.Workbooks(master).Activate With ActiveWorkbook .RefreshAll .RefreshAll .Sheets("BoisePaste").Visible = False .Sheets("BoisePaste").Visible = False End With End Sub 

您需要明确指出您希望Rows.Count和其他此类Range使用( ColumnsRowsCells等)所在的表格。

尝试这个:

 Sheets("BoisePaste").Range(Sheets("BoisePaste").Cells(1,2), Sheets("BoisePaste").Cells(lastRow, 23)).Clear 

所以,通过你的代码,并确保你到处做…即在.Range(.Cells(1,1), .Cells((.Cells(Rows.Count, "A").End(xlUp).Row),22)).Copy ,你没有做到Rows.Count ,所以也添加表单,以防止任何意外的行动。

也许可以这样想,就行了
myVariable = Sheets("mySheet").Range(Cells(1,1),Cells(1,2)).Value

VBA正在阅读

mySheet ,查找范围。 什么范围? 嗯,用户说Cells(1,1)Cells(1,2) ,但他要什么表? 当前的activesheet被称为yourSheet …他指定的Range应该是(表名为mySheet ),但他没有在Cells() ,所以我不知道他想要什么! mySheet cells(1,1)yourSheet cells(1,1)

(是的,这正是一台电脑的想法:P)

编辑:我经历了,并试图帮助收紧你的代码。 但是,正如你所看到的,也许我不太看好你想做什么,但这应该给你一些帮助/见解:

 Private Sub CommandButton1_Click() Dim x As Integer Dim boisePaste As Integer Dim jrgPaste As Integer Dim master As Integer Dim lastRow As Integer Dim bookCount As Integer bookCount = Application.Workbooks.Count ' Create variables to hold the workbook and sheet names. Dim jrgWS As Worksheet, boiseWS As Worksheet Dim masterWB As Workbook Set masterWB = Workbooks(master) Set jrgWS = Sheets("JRGPaste") Set boiseWS = Sheets("BoisePaste") For x = 1 To bookCount If Left(Application.Workbooks(x).Name, 14) = "ITEM_INVENTORY" Then boisePaste = x ElseIf Left(Application.Workbooks(x).Name, 6) = "report" Then jrgPaste = x ElseIf Left(Application.Workbooks(x).Name, 8) = "Portland" Then master = x End If Next x 'Unhide sheets and delete Boise range' Application.ActiveWorkbook.Sheets("BoisePaste").Visible = True jrgWS.Visible = True With boiseWS lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row .Range(.Cells(1, 2), .Cells(lastRow, 23)).Clear End With 'Open Boise file and copy range, paste in master' '' DONT USE ACTIVE SHEET! Use your variables instead 'Application.Workbooks(boisePaste).Activate With boiseWS 'Since you want values (xlPasteValues), just set the two ranges equal instead of copy/paste .Range("B1").Value = .Range(.Cells(1, 1), .Cells((.Cells(.Rows.Count, "A").End(xlUp).Row), 22)).Value End With 'Open JRG report and copy range, paste in master' ' The below just pastes into the same sheet, no?? jrgWS.Cells.Copy jrgWS.Range("A1").Paste Application.CutCopyMode = False 'Refresh pivot tables; hide sheets' Application.Workbooks(master).Activate With ActiveWorkbook .RefreshAll .RefreshAll .Sheets("BoisePaste").Visible = False End With End Sub