VBA Run Time Error 1004: Method “Range” of object’ _ Global’ failed

manuvizcarra

New Member
Joined
Jun 14, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello MrExcel community, I´m new to VBA and I´d like to know if someone knows what´s wrong with my code.

The code should copy a range from workbook "ReporteCNG.xlsx" and paste it in workbook "Consumos CNG1.xlsx".

However, when I run into an error in the line where I try to copy the range from Workbook "ReporteCNG.xlsx") Worksheet("Sheet1")

Sub Copy_Paste_CNG()

Workbooks.Open "C:\Users\manuv\Documents\PELSA\Consumos CNG1.xlsx"

Dim fila As Long
Dim cng As Worksheet
Dim desc As Worksheet
Dim prueba As Range

fila = Cells(Rows.Count, 1).End(xlUp).Row
Set cng = Workbooks("Consumos CNG1.xlsx").Worksheets("General")
Set desc = Workbooks("ReporteCNG.xlsx").Worksheets("Sheet1")
Set prueba = Workbooks("Consumos CNG1.xlsx").Worksheets("Hoja1").Range("A1")

desc.Range(Cells(2, 1), Cells(fila, 18)).Copy prueba
Captura.JPG

Workbooks("Consumos CNG1.xlsx").Close SaveChanges:=True

End Sub[/CODE]

Thank you for your help!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello MrExcel community, I´m new to VBA and I´d like to know if someone knows what´s wrong with my code.

The code should copy a range from workbook "ReporteCNG.xlsx" and paste it in workbook "Consumos CNG1.xlsx".

However, when I run into an error in the line where I try to copy the range from Workbook "ReporteCNG.xlsx") Worksheet("Sheet1")

Sub Copy_Paste_CNG()

Workbooks.Open "C:\Users\manuv\Documents\PELSA\Consumos CNG1.xlsx"

Dim fila As Long
Dim desc As Worksheet
Dim prueba As Range

fila = Cells(Rows.Count, 1).End(xlUp).Row
Set desc = Workbooks("ReporteCNG.xlsx").Worksheets("Sheet1")
Set prueba = Workbooks("Consumos CNG1.xlsx").Worksheets("Hoja1").Range("A1")

desc.Range(Cells(2, 1), Cells(fila, 18)).Copy prueba
View attachment 40792
Workbooks("Consumos CNG1.xlsx").Close SaveChanges:=True

End Sub[/CODE]

Thank you for your help!
 
Upvote 0
Hi,​
do not forget to add the worksheet reference to each Cells statement of this codeline …​
 
Upvote 0

Can be avoided using With block statement rather than a worksheet variable …​
 
Upvote 0

Forum statistics

Threads
1,221,813
Messages
6,162,117
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top