Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign a number in excell 1

Status
Not open for further replies.

funkjunky

Vendor
Jan 22, 2002
28
0
0
GB
I want to assign a number in excel that will increase every time the work book is opened. This is to be used as an invoice number. Where can I get the code for this?
 
put it in an autorun macro. a Macro that always executes when the file is opened.

I can't remember what I called it but in Access & Excel they are called different things. Autorun or Auto_Open or AutoExec try those words in the help file. It is well hidden - I will post when I find it - though I expect others will get here first.

I am very wary of them because that is mechanism the "I love you" Melissa virus used. In short I treat all e-mailed .doc .xls .dbm etc files with suspician.
 
call the macro Auto_Open in Excel (AutoExec in Access)

Sub Auto_Open()
Sheets("sheet1").Range("A1").Value = Sheets("sheet1").Range("A1").Value + 1
End Sub

the sheet and cell value should be obvious.

If you are new to macros I strongly recommend using the record function and take a gander at what Excel thinks you havejust done manually. Even years later i find it easier to do this for anything that is accessable from the mouse.

A warning though - if having difficulty with a manual v recorded sequence read the help file in detail - the Macro record is not infallible!!!! I have faulted it several times.
 
Spoke to Soon. The problem is it only works if you save the work sheet. This defeats the object a bit as you would have to remove all the data entered in the sheet B4 closing. any way around this?
 
Add a new sheet just for this purpose?
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
IF the ONLY time that the spreadsheet is being opened is through your program, then it would be very simple to use the SaveSettings and GetSettings methods in VB.Asign the number from GetSettings to a global level variable, then before you actually assign to SaveSettings, get the number from the global variable add 1 then save it back to the registry. Doing this in VB obviously will not work if the spreadsheet has the ability to be opened at any time other than through your program. Look in Excel help for save/get settings .. perhaps there is a method to accomplish the same thing that is exclusive to Excel.
 
Am I missing something here?
Surely if you add a save instruction immediately after the index instruction (don't "save as" unless you set an ignore replies mode - no I can't remember the instruction)

Oh of course what about the Auto_Close macro?

Sub Auto_Close
Sheets("sheet2").Range("a1:zz1000").Clear
ActiveWorkbook.Save
End Sub

Choose your range

In help go look for AutoActivate and see also
 
This is nearly there. The project is a sales invoice. with the "dont save as" command all the sheet is saved when you close. We need to be able to close the sheet without saving the info entered, but for the invoice number to advance by 1 the next time you open the work book. The work book is not opened by a program just manually opened by the operator when requiered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top