Run time error 5- Invalid procedure call or argument

Cummins

Board Regular
Joined
Jul 26, 2011
Messages
58
Error occurs at the "Name sfile as sbfile" line. Is there an object issue? Is there an issue with the use of date and time in the sbfile format?


Public Sub Print_Batch_RS()
Dim sfile As String
Dim sbfile As String
Dim Myfile As String
Dim MyoldFile As String
Dim g
Dim g2
Dim MyDate
Dim MyTime
Application.ScreenUpdating = False

'****Establish the file BatchRecords to put the data into
g = Date
g2 = Time
Myfile = "\BatchRecords.xls"
MyDate = Format(g, "yymmdd")
MyoldFile = "\BtchRcrd_" & MyDate & "_" & g2 & ".xls"
Set sfile = MyPath & Myfile
Set sbfile = MyPath & MyoldFile

If Dir(sfile) <> "" Then 'Checks to see if the file exists
On Error Resume Next
Workbooks("BatchRecords.xls").Close SaveChanges:=True 'Closes file and saves it if it is open
On Error GoTo 0
Name sfile As sbfile 'There is no need to Kill sfile as it no longer exists

Set Newbook = Workbooks.Add 'If it is not found, this creates and opens the file.
With Newbook
.Title = "Summary Data for AIM."
.Subject = "Data converted from raw AIM output files."
.Author = "Clark Cummins"
.SaveAs FileName:=sfile
' .Worksheets(1).???
End With

End If
Workbooks.Open sfile 'Opens the new file
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
- You do not Set to fill a String variable, but you use Set for object variables.

- BTW, what is the contents of the variable MyPath? Where is it populated?

Please use
Code:
 tags when you paste code on the forum. Thanks for consideration.
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,845
Members
449,471
Latest member
lachbee

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