Avatar of rimmy
rimmy
 asked on

I Am getting an error 1004 Application-defined or object-defined . please answer me how to remove this error and execute my programe.

My code is:
In Module 1-

Sub copyDataFromMultipleWorkbook()
Dim FolderPath As String
Dim Filepath As String, Filename As String
FolderPath = "C:\Myprg\"

Filepath = "*data1.xlsx*"

Filename = Dir(Filepath)

Dim lastrow As Long, lastcolumn As Long

Do While Filename <> ""
Workbooks.Open (FolderPath & Filename)

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 4))

Filename = Dir
Loop
Application.DisplayAlerts = True
End Sub

In Module 2-

Sub cpyDataFromMultipleWorkbook()

Dim FolderPath As String
Dim Filepath As String, Filename As String

FolderPath = "C:\Myprg\"

Filepath = "*data2.xlsx*"

Filename = Dir(Filepath)

Dim lastrow As Long, lastcolumn As Long

Do While Filename <> ""
Workbooks.Open (FolderPath & Filename)

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close

erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet2").Range(Cells(erow, 1), Cells(erow, 4))

Filename = Dir

Loop
Application.DisplayAlerts = True
End Sub
Programming

Avatar of undefined
Last Comment
rimmy

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
rimmy

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
rimmy

ASKER
please  reply soon I don't Know how remove this error.
The Programe is I want to copy : File1 to sheet1 of master file &
                                                           File2 to sheet2 of Master file.
My error is comming here
ActiveSheet.Paste Destination:=Worksheets("Sheet2").Range(Cells(erow, 1), Cells(erow, 4))

=> i wrote thi code also:
Private Sub Workbook_Open()

copyDataFromMultipleWorkbook
 
    cpyDataFromMultipleWorkbook

End Sub
Your help has saved me hundreds of hours of internet surfing.
fblack61