Renaming workbook

Useless1

New Member
Joined
Jun 8, 2010
Messages
12
Hi
I would like to find a way to have my invoice workbook automatically rename using 2 different cells, one of which is an invoice number in cell N6 generated using this code:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Range("N6").Value = Range("N6").Value + 1
End Sub

the other being text in cell N11. The renamed workbook should look like "Inv N6-N11.xls"
Its probably best if this happens when saving the workbook.

Thanks in advance
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this

Code:
ActiveWorkbook.SaveAs filename:="C:\" & Range("N6") & "-" & Range("N11") & ".xls", FileFormat:= _
        xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
        , CreateBackup:=False

You can change C:\ to a path to suit you
 
Upvote 0
Thanks pboltonchina, unfortunately the problem has become more complex. I managed to solve the saving and renaming with this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.EnableEvents = False
Me.SaveAs "Inv" & " " & Range("N6").Value & "-" & Range("N11").Value & ".xls"
Cancel = True
Application.EnableEvents = True
End Sub

Which works perfectly, however the automatic sequential inv no. doesnt work as it saves the higher no. to the new file rather than the old template, to do what I want I need some code that will fit in the above sub that opens the template "Inv Template.xls" adds 1 to the N6 cell and then saves and closes it. Is this possible?

Thanks
 
Upvote 0
Thanks Andrew, the thread you directed me to has an auto generated no. upon opening, I think I need the number generated at the save point, which is why I thought it might fit into the code above. Sorry i'm a true novice at this and need very precise instructions. p.s. am on a mac if that makes a difference.
 
Upvote 0
Hi Andrew

I think if I do that then every save will be 1 number higher than the template, I.E. all the same.

So I thought that in the Beforesave sub I could add code that opened "inv template.xls", added 1 to the cell, saved as "inv template.xls", replacing original, then closed. This would have to happen after the rename in the sub.

Thanks
 
Upvote 0
The code in the link I posted puts the next number in A1. Can't you use that in the file name? You don't need to save the template.
 
Upvote 0
hi andrew tried that code on a new workbook to see how it worked and got

runtime-error '424':
object required

this is the line highlighted in debug

FName = ThisWorbook.Path & Application.PathSeparator & "Number.Txt"

thanks
 
Upvote 0

Forum statistics

Threads
1,215,544
Messages
6,125,444
Members
449,226
Latest member
Zimcho

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