Need help with my Code

epze89

New Member
Joined
Nov 21, 2016
Messages
8
hey guys I'm in need of your help,
my Code keep crashing and i Donno why. i have on one sheet table that i got from SQL
and I have imported the table to the sheet called "DataSrc" ,now when I'm trying to loop through the table
its crashing in random places i get the 1004 error.
i need to merge all .xls file from the same Clint to one excel with sheets
its opening the file starting to run like it's all good then all of a sudden crash please help me.
Code:
Sub DoFolder()
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim MyPath, MyType As String
Dim MyDate As Date
Dim strFilename As String
Dim Name As String
Dim i As Long
   i = 2
   With ThisWorkbook.Sheets("DataSrc")
    Application.DisplayAlerts = False
    Set wbDst = Workbooks.Add
    Do Until .Range("E" & i) = ""
    MyPath = .Range("G" & i)
    MyDate = .Range("B" & i)
     If Range("C1") <= MyDate Then
     If .Range("G" & i) = .Range("G" & i - 1) Or i = 2 Then
Zibi:
                 Application.EnableEvents = False
            Set wbSrc = Workbooks.Open(MyPath)
                
            Set wsSrc = wbSrc.Worksheets(1)
          
              ActiveSheet.Name = .Range("C" & i)
 
          
            wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
 
            wbSrc.Close False
Else
    wbDst.Worksheets("Sheet1").Delete
Name = .Range("A" & i)
wbDst.SaveAs "C:\Users\Elia.Peretz\Desktop\גירסאות פרוייקט\27\" & Name & " & " & i & ".xlsx "
wbDst.Close True
Set wbDst = Workbooks.Add
GoTo Zibi
End If
 
   End If
i = i + 1
Loop
wbDst.Close False
End With
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

HackSlash

Active Member
Joined
Nov 18, 2016
Messages
360
You have to tell us exactly what line it is crashing on. Nobody can help you fix "random places". In my experience 1004 error can sometimes be caused by bugs in excel. Try to update office. Install all the latest patches and then tell us exactly what line is causing the problem.
 
Upvote 0

epze89

New Member
Joined
Nov 21, 2016
Messages
8
You have to tell us exactly what line it is crashing on. Nobody can help you fix "random places". In my experience 1004 error can sometimes be caused by bugs in excel. Try to update office. Install all the latest patches and then tell us exactly what line is causing the problem.
i know i have tried to run it like 1000 times and it crashes on different files all I'm asking if u have a better way to Wright the code so i can test your experienced code and get it to work
tyvm
 
Upvote 0

epze89

New Member
Joined
Nov 21, 2016
Messages
8
You have to tell us exactly what line it is crashing on. Nobody can help you fix "random places". In my experience 1004 error can sometimes be caused by bugs in excel. Try to update office. Install all the latest patches and then tell us exactly what line is causing the problem.

is it possible that exel opening and saving is limited to 50 files because its crashing exactly in file 51 i get error 1004
and if that's the case there is a way around it?
 
Upvote 0

HackSlash

Active Member
Joined
Nov 18, 2016
Messages
360
You can test that by opening 51 spreadsheets and see what happens. It could also be that your computer has filled up the available memory. Open Task Manager and watch your memory usage while the code runs.
 
Upvote 0

epze89

New Member
Joined
Nov 21, 2016
Messages
8
You can test that by opening 51 spreadsheets and see what happens. It could also be that your computer has filled up the available memory. Open Task Manager and watch your memory usage while the code runs.
Ty i got it to work ill post my working code if some one want to use it
 
Upvote 0

Forum statistics

Threads
1,195,923
Messages
6,012,314
Members
441,690
Latest member
CyberWrek

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