VBA Runtime Error 1004 sur Range.Clear

Il y a beaucoup de discussions sur cette erreur, mais je ne peux pas faire fonctionner, peu importe ce que j'essaie. La plupart des gens disent qu'il se produit lorsque vous essayez d'invoquer une méthode sur une feuille inactive, mais vous ne devriez pas avoir à le faire. L'erreur se trouve à la ligne 28. Merci.

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 

Vous devez indiquer explicitement quelle feuille vous voulez que le Rows.Count et d'autres Rows.Count utilisent (les Columns , les Rows , les Cells , etc.) seront Rows.Count .

Essaye ça:

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

Donc, passez par votre code et assurez-vous de le faire partout … c.-à-d. À l' .Range(.Cells(1,1), .Cells((.Cells(Rows.Count, "A").End(xlUp).Row),22)).Copy de .Range(.Cells(1,1), .Cells((.Cells(Rows.Count, "A").End(xlUp).Row),22)).Copy , vous ne l'avez pas fait à Rows.Count , alors ajoutez la feuille là-bas, pour éviter toute action inattendue.

Pensez-y comme cela peut-être, avec la ligne
myVariable = Sheets("mySheet").Range(Cells(1,1),Cells(1,2)).Value

VBA lit cela comme

Dans mySheet , searchz une gamme. Quelle gamme? Hm, l'user dit Cells(1,1) et Cells(1,2) , mais quelle feuille veut-il cela? La feuille de travail actuelle s'appelle yourSheet … Il a spécifié où la Range devrait être (feuille appelée mySheet ), mais il n'a pas sur Cells() , donc je ne sais pas ce qu'il veut! mySheet cells(1,1) ou yourSheet cells(1,1) ??

(et oui, c'est exactement ce qu'un ordinateur pense: P)

Edit: J'ai traversé et essayé d'aider à resserrer votre code. Mais, comme vous le voyez peut-être, je ne suis pas très positif quant à ce que vous voulez faire, mais cela devrait vous donner un peu d'aide:

 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