SmartCookie
New Member
- Joined
- Feb 22, 2021
- Messages
- 6
- Office Version
- 2019
- Platform
- 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)
2)
3)
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