error handling

cazdealer

Board Regular
Joined
Mar 12, 2011
Messages
96
Hi,
Everytime I run this code, it works on the first i but when it does the loop, on the next i, my excel crashes or I got an error 13.


first i = Louis101.xlsm, next i = Louis102.xlsm... etc. there are 20 "i"s.

I think it crashes when on the second loop, it reads the line: Application.Run nameofthedata & "!start"
but I'm not sure about that.

what I don't understand is if I try my macro a second time by switching i to 2, it will work the first time again but crashed on next i.


Do you know where is the problem?

Code:
Sub weekend()


On Error Resume Next


Dim nameofthedata As String
Dim i As Integer



i = i

For i = 1 To 20


nameofthedata = Worksheets("DATAlist").Cells(i, 1).Value


     

Workbooks.Open Filename:= _
        "C:\Users\Louis\Documents\backup\DATA\" & nameofthedata & ""
        
       
        ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\Louis\Documents\results\DATA\" & nameofthedata & "", FileFormat _
       :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


Application.Run nameofthedata & "!start"


Workbooks(nameofthedata).Close savechanges:=True

ThisWorkbook.Activate


    nameofthedata = 0
  
    Next i
    
  
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You are setting
Code:
nameofthedata = 0
'as an integer
after declaring it as a string
Code:
Dim nameofthedata As String

If you want to clear it, which isn't really needed,
Code:
nameofthedata = ""
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,898
Members
452,948
Latest member
Dupuhini

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