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
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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
 
Upvote 0
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!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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