Run-time Error '1004': Application-defined or object-defined error.

whoosh

New Member
Joined
May 16, 2018
Messages
17
Dear Gurus,

The above-mentioned error keeps popping up when I execute the code below. It was running perfectly fine till I switched the sourceWB workbook and made changes to the code to reflect the change.

Since the change, i could not get the macro to run and it is driving me crazy.
Hope someone can help to highlight what could be the possible cause of this error.


Thanks in advance.


Code:
Option Explicit

Sub CopyExtRes_FRBO()
'
' Copy from FRBO
'
Dim sourceWB As Workbook, targetWB As Workbook, ws1 As Worksheet, i As Integer
Dim nRow As Long, lRow As Long, sht As String, sheet As Worksheet


Application.ScreenUpdating = False


Set sourceWB = Workbooks("FRBO (Private Apt).xls")
Set targetWB = Workbooks("ck's database_Residential (updating 01 May 2018).xlsm")
Set ws1 = sourceWB.Sheets("Sheet1")
lRow = ws1.Cells(Rows.Count, "B").End(xlUp).Row - 2
With ws1
    For i = 2 To lRow
        sht = .Range("B1") & .Range("B" & i)
            For Each sheet In targetWB.Sheets
                If sheet.Name = sht Then
                    With sheet
                        nRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                        ws1.Range("A" & i).Copy Destination:=.Range("N" & nRow)
                        ws1.Range("C" & i).Copy Destination:=.Range("A" & nRow)
                        ws1.Range("D" & i).Copy Destination:=.Range("R" & nRow)
                        ws1.Range("E" & i).Copy Destination:=.Range("S" & nRow)
                        ws1.Range("F" & i).Copy Destination:=.Range("E" & nRow)
                        ws1.Range("G" & i).Copy Destination:=.Range("M" & nRow)
                        ws1.Range("H" & i).Copy Destination:=.Range("H" & nRow)
                        ws1.Range("I" & i).Copy Destination:=.Range("J" & nRow)
                        ws1.Range("J" & i).Copy Destination:=.Range("O" & nRow)
                        ws1.Range("K" & i).Copy Destination:=.Range("U" & nRow)
                    End With
                End If
            Next sheet
    Next i
End With
Application.ScreenUpdating = True


End Sub
 
Thanks RasGhul, i checked that many times. File names are correct, and the worksheet as well.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Unfortunately you have have to retrace your steps back through your changes. Does your code still work if you revert back to your original sourceWB file?
 
Upvote 0
I think I might have found the problem.
Column B has a header "DT" in the older version file "Res Source wb.xls", but the new version "FRBO (Private Apt).xls" has a header "D" instead. Is it possible to tweet the code to make it run?

Please advise. Thanks!


Code:
Option Explicit

Sub CopyExtRes_FRBO()
'
' Copy from FRBO
'
Dim sourceWB As Workbook, targetWB As Workbook, ws1 As Worksheet, i As Integer
Dim nRow As Long, lRow As Long, sht As String, sheet As Worksheet


Application.ScreenUpdating = False


Set sourceWB = Workbooks("FRBO (Private Apt).xls")
Set targetWB = Workbooks("ck's database_Residential (updating 01 May 2018).xlsm")
Set ws1 = sourceWB.Sheets("Sheet1")
lRow = ws1.Cells(Rows.Count, "B").End(xlUp).Row - 2
With ws1
    For i = 2 To lRow
        sht = .Range("B1") & .Range("B" & i)
            For Each sheet In targetWB.Sheets
                If sheet.Name = sht Then
                    With sheet
                        nRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
                        ws1.Range("A" & i).Copy Destination:=.Range("N" & nRow)
                        ws1.Range("C" & i).Copy Destination:=.Range("A" & nRow)
                        ws1.Range("D" & i).Copy Destination:=.Range("R" & nRow)
                        ws1.Range("E" & i).Copy Destination:=.Range("S" & nRow)
                        ws1.Range("F" & i).Copy Destination:=.Range("E" & nRow)
                        ws1.Range("G" & i).Copy Destination:=.Range("M" & nRow)
                        ws1.Range("H" & i).Copy Destination:=.Range("H" & nRow)
                        ws1.Range("I" & i).Copy Destination:=.Range("J" & nRow)
                        ws1.Range("J" & i).Copy Destination:=.Range("O" & nRow)
                        ws1.Range("K" & i).Copy Destination:=.Range("U" & nRow)
                    End With
                End If
            Next sheet
    Next i
End With
Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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