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
 

Some videos you may like

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,402
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,402
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
 

Watch MrExcel Video

Forum statistics

Threads
1,118,525
Messages
5,572,641
Members
412,478
Latest member
MakeItWorkVBA
Top