Only execute auto open if certain criteria are met

EJB

New Member
Joined
Jun 4, 2004
Messages
18
I have the following code in a worksheet I am using to raise purchase orders. The worksheet is called Purchase Order.xlt

Sub auto_open()
NewOrderNumber
RenameFile
End Sub

Private Sub NewOrderNumber()
Range("ref").Copy
Range("ref2").PasteSpecial Paste:=xlValues, _
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveWorkbook.Save
End Sub

Private Sub RenameFile()
Dim myOrderNumber As Range
Set myOrderNumber = Range("OrderNumber")

ActiveWorkbook.SaveAs
("R:\............\Purchase Order " & myOrderNumber & ".xls")
End Sub

This works fine as its creating a new file for each order, the problem I have is when I reopen the order for review - it renames it!

I need something that says 'if I've already been numbered don't do it again' or probably more simplistically something like "If I am called *.xlt then rename me, but if I am called *.xls then don't"

Can someone help me with this.

Many thanks
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,686
You just need to specify the fileformat as xlNormal in your SaveAs statement, else Excel will save it as type template ( even though you are naming it with an extension of .xls ).
 

EJB

New Member
Joined
Jun 4, 2004
Messages
18
Thanks for this, I take your point and I have amended, but it hasn't resolved my problem. The code to resave the file happens everytime the file opens. I'm a real beginner with this stuff, and the only way I can think of dealing with it is to put an error handler in to say if the new file name already exists, ignore the error and exit the sub, but this feels a bit like using a sledge hammer to crack a nut. Is there a nice way of saying 'if I've already been renamed and saved, don't do it again?' It does need to be on the auto open function because I don't want the users to have to remember to rename the file.

Thanks
 

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,686
You could test the fileformat of the current workbook, at the beginning of the RenameFile sub, and only continue processing is the filetype is xlTemplate.

Use something like
If ThisworkBook.FileFormat <> xlTemplate Then Exit Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,812
Messages
5,833,801
Members
430,233
Latest member
cbutts

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