I'm an Excel VBA newbie trying to copy a range of cells from several worksheets and I want to paste this content on another worksheet in the same workbook by finding the last empty row and paste it there going one by one on each worksheet.
When I execute my code I get a 1004 error. I tried to test different sections of my code and I noticed that when I try to select a cell in a worksheet, I also get the same 1004 error but now says the following "Error in select method of Range class".
I tried using different methods to select this cell in my worksheet like "ThisWorkbook" "ActiveWorkbook" with "Sheets" and "Worksheets" and for whatever reason, I get the same error.
The code is in a module and I call the execution of this code using a button.
The interesting thing is that when I select the worksheet first and then select the range, I don't get this problem.
Like this:
ThisWorkbook.Sheets(3).Select
ThisWorkbook.Sheets(3).Range("A6").Select
I'd like to understand why this is the case so I can avoid using select in my code.
By the way, English is not my native language in case I didn't translate the errors that Visual Basic displays correctly.
Here is my code:
Sub ConsolidacionReportes()
Dim Fila As Long ' to count each row in a for loop
Dim UltimaFila As Integer ' Used to find the last row of a sheet
Dim Hojas As Integer ' used in a for loop to go through each sheet in the workbook
Dim RangoCeldas As String ' Range of cells
Dim BorrarTotales As Long ' another variable to count rows
For Hojas = 2 To 6
UltimaFila = Sheets(Hojas).Cells(Sheets(Hojas).Cells.Rows.Count, "C").End(xlUp).Row - 1
For Fila = UltimaFila To 5 Step -1
RangoCeldas = "C"
If Sheets(Hojas).Cells(Fila, "C") = "" Then
Sheets(Hojas).Rows(Fila).Delete
End If
Next
Next
For Hojas = 2 To 6
BorrarTotales = Sheets(Hojas).Cells(5, 3).End(xlDown).Row + 3
For Fila = BorrarTotales To 5 Step -1
If Sheets(Hojas).Cells(Fila, "C") = "" Then
Sheets(Hojas).Rows(Fila).Delete
End If
Next
Next
Reporte1.Range("A1:I5").Copy Calculos.Range("A1")
' When trying to select this range I get the error 1004: Error in select method of Range class
ThisWorkbook.Sheets(3).Range("A6").Select
For Hojas = 2 To 6
UltimaFila = Sheets(Hojas).Cells(5, 1).End(xlDown).Row
Let RangoCeldas = "A6:" & "B" & UltimaFila
' When executing the code I get another error 1004, this time the description is: Error defined by the application or the object
Sheets(Hojas).Range(RangoCeldas).Copy Calculos.Cells(Calculos.Cells(5, 3).End(xlDown), 1)
Next
End Sub
Thisworkbook
isn't active when you try and select a cell in it. – Darren Bartrup-Cook