Always running into Run-Time Error '1004'

SmartCookie

New Member
Joined
Feb 22, 2021
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone, I have been running into a VBA runtime 1004 'Application-Defined or object defined error'

Below are the code that have the issue. The weird thing is that when I open the workbook they will never run the first time, but after I run it couple times, they will magically happened to work. (I dont know what I have changed in between but they just work), that also proved the code is not written incorrectly. Another thing I observed is that they are all copy&paste vba, mainly copying one range of the data from one workbook to another workbook. I suspected there might be something to do with the variable assignment but can't be sure. Please help! Thank you.

1)

VBA Code:
Sub CopyRawData_RSWW()
''
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("Base Data_RS")
Set wb2 = Workbooks.Open("C:\Users\DengSilin\Desktop\Material & Information\Bklg_Inv_Match_FL(SJC)_021721.xlsm")
Set ws2 = wb2.Worksheets("WIP-RSWW")
EndofRow = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row


ws2.Range("A10", Cells(EndofRow, 5)).Copy
ws1.Range("A4").PasteSpecial
Application.CutCopyMode = False

End Sub


2)
VBA Code:
Sub CopyRawData_CRSWW()

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("C:\Users\DengSilin\Desktop\Material & Information\Bklg_Inv_Match_FL(SJC)_021721.xlsm")
Set ws3 = wb1.Worksheets("Base Data_CRS")
Set ws4 = wb2.Worksheets("WIP-CRSWW")
EndofRow = ws4.Cells(ws4.Rows.Count, 1).End(xlUp).Row
ws4.Range("A10", Cells(EndofRow, 5)).Copy
ws3.Range("A4").PasteSpecial
Application.CutCopyMode = False

End Sub

3)
VBA Code:
Sub CopytoFinalSheet()
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("Template")
Set ws2 = wb1.Worksheets("IFX WW19")
EndofRow = ws1.Cells(ws1.Rows.Count, 2).End(xlUp).Row

ws1.Range("B2", Cells(EndofRow, 6)).Copy
ws2.Range("B14").PasteSpecial

Application.CutCopyMode = False

End Sub
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,549
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
You have the same problem in each sub, namely unqualified ranges. This
VBA Code:
ws2.Range("A10", Cells(EndofRow, 5)).Copy
should be
VBA Code:
ws2.Range("A10", ws2.Cells(EndofRow, 5)).Copy
 

SmartCookie

New Member
Joined
Feb 22, 2021
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
Hi & welcome to MrExcel.
You have the same problem in each sub, namely unqualified ranges. This
VBA Code:
ws2.Range("A10", Cells(EndofRow, 5)).Copy
should be
VBA Code:
ws2.Range("A10", ws2.Cells(EndofRow, 5)).Copy
Thank you!! You are right. changed it and it worked perfectly!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,549
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,589
Messages
5,625,671
Members
416,125
Latest member
NeedExcelHelp2021

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
Top