Sequential Numbering from Blank Document (k)

W

W. Kirk Lutz

I am trying to create an Excel master document that when opened,
automatically creates a new version of itself with a number that grows
with each open.

So if the number starts at 00000 and the master template document is
opened as an Excel sheet opens with that number as 00001

What do I put in the number field for this to happen?

-Kirk
 
B

Bernard Rey

W. Kirk Lutz said:
I am trying to create an Excel master document that when opened,
automatically creates a new version of itself with a number that grows
with each open.

So if the number starts at 00000 and the master template document is
opened as an Excel sheet opens with that number as 00001

What do I put in the number field for this to happen?

Find the tip on JE's page:

http://www.mcgimpsey.com/excel/udfs/sequentialnums.html
 
W

W. Kirk Lutz

Thanks for the link. I am, however, a major novice when it comes to
Macros and actually anything other than adding numbers in Excel.

I'm not sure how to use what he wrote.

Could you write it up as if I'm from another planet?

-Kirk
 
J

JE McGimpsey

W. Kirk Lutz said:
Thanks for the link. I am, however, a major novice when it comes to
Macros and actually anything other than adding numbers in Excel.

I'm not sure how to use what he wrote.

Could you write it up as if I'm from another planet?

Take a look at David McRitchie's "Getting Started with Macros and User
Defined Functions":

http://www.mvps.org/dmcritchie/excel/getstarted.htm

It's written from a WinXL-centric perspective, but most everything is
transferable.

Let us know if that doesn't help...
 
J

J Laroche

Bernard Rey wrote on 2005/01/10 17:55:


If modifying JE's excellent code (I'm still discovering something:
SaveSetting) to adapt to your situation is out of your or your friends'
reach, select and copy the code below, and paste it in your master
workbook's code. To access that code, control-click on the workbook's title
bar and select View Code. Remove the empty Workbook_Open procedure, if there
is one, before pasting.

Only a few things have to be verified after pasting: the constants
InitialValue, increment, prefix and suffix, and the selection of saving
method, near the bottom. Comment out the unwanted one. Method 1 will
automatically save the new workbook in the same folder than the master file,
while method 2 will ask each time where to save (the file can be renamed at
this point; saving can be cancelled, however the sequential number is still
increased). That's up to you, but do it before using the master file for the
first time.

JL
Mac OS X 10.3.7, Office v.X 10.1.6




Private Sub Workbook_Open()
SequentialNumber
End Sub

Sub SequentialNumber()
' JL 2005-01-10
' On each opening of a master file, create a new file named
' with a sequential number

Const InitialValue = 1 ' first sequential number to use
Const increment = 1 ' increment from one sequential number to the next
Const prefix = "" ' text to put before the sequential number
Const suffix = "" ' text to put after the sequential number

Dim existSN As Boolean
Dim existMF As Boolean
Dim master As Boolean
Dim cdp As DocumentProperty
Dim SeqNumber As Long, OldSeqNumber As Long
Dim FSName As Variant

existSN = False
existMF = False
master = False

With ThisWorkbook

For Each cdp In .CustomDocumentProperties
If cdp.Name = "Sequential Number" Then
existSN = True
OldSeqNumber = cdp
cdp = cdp + increment
SeqNumber = cdp
ElseIf cdp.Name = "Master File" Then
existMF = True
master = cdp
End If
Next

' Creation of the custom properties the first time the master
' file is open
' Each property is tested separately in case somebody
' removed a property
If Not existSN Then
.CustomDocumentProperties.Add Name:="Sequential Number", _
LinkToContent:=False, Type:=msoPropertyTypeNumber, _
Value:=InitialValue
SeqNumber = InitialValue
End If
If Not existMF Then
.CustomDocumentProperties.Add Name:="Master File", _
LinkToContent:=False, Type:=msoPropertyTypeBoolean, _
Value:=True
master = True
End If

If master Then ' save a new file only if the master file is open

.Save ' save the master file containing the new seq. number

FSName = prefix & Format(SeqNumber) & suffix
.CustomDocumentProperties.Item("Sequential Number") = _
OldSeqNumber
.CustomDocumentProperties.Item("Master File") = False

' Two methods for saving the new file.
' Put an apostrophe in front of the unnecessary method's lines
' before first use.

' Method 1: new file in same directory than master file
.SaveAs FileName:=.Path & Application.PathSeparator & FSName
' end of method 1

' Method 2: Save path requested from user
FSName = Application.GetSaveAsFilename(InitialFilename:=FSName)
If FSName <> False Then
.SaveAs FileName:=FSName
Else
.Saved = True
' to avoid a dialog box if immediately closed
End If
' end of method 2

End If

End With

End Sub
 
W

W. Kirk Lutz

You lost me after pasting the code into the master workbook code.

I don't know how to verify constants or comment out the rest. I don't
know what I'm looking at.

How do I use this? I currently have a cell with 00000 in it. How do I
get this cell to use this script? So the next time the template is
opened the number is 00001?

Sorry, but I have never done anything other than add or multiply numbers
in Excel. This is way beyond me.

-Kirk
 
W

W. Kirk Lutz

I also posted this on MacFixit.com and someone sent this code:

Private Sub Workbook_Open()
Sheet1.[A1] = Sheet1.[A1].Value + 1
End Sub

I changed it to:
Private Sub Workbook_Open()
Sheet1.[E2] = Sheet1.[E2].Value + 1
End Sub

but it doesn't work quite right. It does increase the number in E2 by 1
(from 4401 to 4402) but it doesn't save over the template so the next
time the number is 4403.

Thoughts?

-Kirk
 
J

J Laroche

OK, there is one thing that needs to be clarified here.
1) I understood that from a master file (as an example, Invoice Master), you
wanted to create subsequent files for each invoice you create. Each of these
files is called for example Invoice 1, Invoice 2, etc. Is that correct?

2) Or do you want that a master file only keeps in a cell a number going up
each time you open that file? Then you take care of saving the file yourself
under a new name?

3) Or do you want a combination of both, i.e. a file automatically bearing a
numbered name plus in one of its cells its own sequential number (like an
invoice would do)?

Now if the case that interests you is 1, then I have an updated version of
the code posted yesterday, because there was a small flaw concerning the way
the sequential number was kept in the properties (I know this is above your
head, but others may be interested). See the new code at the bottom of the
message. It entirely replaces what I posted yesterday.

If it's case 2, then what you got on MacFixIt was almost right. I assume
that you do a Save As, thus losing the value you want to keep in the master
file. See in your previous post how to easily fix it.

If it's case 3, then I'd have to make a few easy changes to my code.

Now, the term "Comment out" means to tell the compiler to ignore certain
lines, to treat them like comments instead of computer instructions. The
compiler ignores lines (or parts of lines) that begin with an apostrophe
('). The comments appear green in the code.

The constants are the lines that begin with the word Const. Look for them
near the top of the code. Can you at least make sense of a line that says:
Const InitialValue = 1 ' first sequential number to use
If you wanted to save your files under the names Invoice 1, Invoice 2 as
mentioned above, then you'd change the following line:
Const prefix = "Invoice " ' text to put before the sequential number

Don't be intimidated by Visual Basic because you've never studied it. Even
though you may not understand everything, you should have a basic
understanding of it - then - end if structures. Read the comments, it helps
too.

JL
Mac OS X 10.3.7, Office v.X 10.1.6


W. Kirk Lutz wrote on 2005/01/11 10:59:
I also posted this on MacFixit.com and someone sent this code:

Private Sub Workbook_Open()
Sheet1.[A1] = Sheet1.[A1].Value + 1
End Sub

I changed it to:
Private Sub Workbook_Open()
Sheet1.[E2] = Sheet1.[E2].Value + 1
ThisWorkbook.Save
End Sub
but it doesn't work quite right. It does increase the number in E2 by 1
(from 4401 to 4402) but it doesn't save over the template so the next
time the number is 4403.

Thoughts?

-Kirk


Option Explicit

Private Sub Workbook_Open()
SequentialNumber
End Sub

Sub SequentialNumber()
' JL 2005-01-10
' update 1, 2005-01-11: sequential number now stays correct in Properties
' On each opening of a master file, create a new file named
' with a sequential number

Const InitialValue = 1 ' first sequential number to use
Const increment = 1 ' increment from one sequential number to the next
Const prefix = "" ' text to put before the sequential number
Const suffix = "" ' text to put after the sequential number

Dim existSN As Boolean
Dim existMF As Boolean
Dim master As Boolean
Dim cdp As DocumentProperty
Dim SeqNumber As Long
Dim FSName As Variant

existSN = False
existMF = False
master = False

With ThisWorkbook

For Each cdp In .CustomDocumentProperties
If cdp.Name = "Sequential Number" Then
existSN = True
SeqNumber = cdp + increment
ElseIf cdp.Name = "Master File" Then
existMF = True
master = cdp
End If
Next

' Creation of the custom properties the first time
' the master file is open
' Each property is tested separately in case somebody
' removed a property
If Not existSN Then
.CustomDocumentProperties.Add Name:="Sequential Number", _
LinkToContent:=False, Type:=msoPropertyTypeNumber, _
Value:=InitialValue
SeqNumber = InitialValue
End If
If Not existMF Then
.CustomDocumentProperties.Add Name:="Master File", _
LinkToContent:=False, Type:=msoPropertyTypeBoolean, _
Value:=True
master = True
End If

If master Then ' save a new file only if the master file is open

.CustomDocumentProperties.Item("Sequential Number") = SeqNumber
.Save ' save master file containing the new sequential number

FSName = prefix & Format(SeqNumber) & suffix
.CustomDocumentProperties.Item("Master File") = False

' Two methods for saving the new file.
' Put an apostrophe in front of the unnecessary method's lines
' before use.

' Method 1: new file in same directory than master file
.SaveAs FileName:=.Path & Application.PathSeparator & FSName, _
AddToMru:=True
' end of method 1

' Method 2: Save path requested from user
FSName = Application.GetSaveAsFilename(InitialFilename:=FSName)
If FSName <> False Then
.SaveAs FileName:=FSName, AddToMru:=True
Else
.Saved = True
' to avoid a dialog box if immediately closed
End If
' end of method 2

End If

End With

End Sub
 
W

W. Kirk Lutz

Okay, here is what I would like:

I have a master Excel template stored on our server with a number in a cell.

When that master Excel template is opened by one of my coworkers(say the
current PO number is 4401), the number in that cell(E2) increases by 1
so that the new files is 4402.

They then save the .xls file anyway they see fit.

The master template .xlt file saves the new number(4402) so that the
next person opening it is greeted with 4403 in cell E2.

-Kirk


J said:
OK, there is one thing that needs to be clarified here.
1) I understood that from a master file (as an example, Invoice Master), you
wanted to create subsequent files for each invoice you create. Each of these
files is called for example Invoice 1, Invoice 2, etc. Is that correct?

2) Or do you want that a master file only keeps in a cell a number going up
each time you open that file? Then you take care of saving the file yourself
under a new name?

3) Or do you want a combination of both, i.e. a file automatically bearing a
numbered name plus in one of its cells its own sequential number (like an
invoice would do)?

Now if the case that interests you is 1, then I have an updated version of
the code posted yesterday, because there was a small flaw concerning the way
the sequential number was kept in the properties (I know this is above your
head, but others may be interested). See the new code at the bottom of the
message. It entirely replaces what I posted yesterday.

If it's case 2, then what you got on MacFixIt was almost right. I assume
that you do a Save As, thus losing the value you want to keep in the master
file. See in your previous post how to easily fix it.

If it's case 3, then I'd have to make a few easy changes to my code.

Now, the term "Comment out" means to tell the compiler to ignore certain
lines, to treat them like comments instead of computer instructions. The
compiler ignores lines (or parts of lines) that begin with an apostrophe
('). The comments appear green in the code.

The constants are the lines that begin with the word Const. Look for them
near the top of the code. Can you at least make sense of a line that says:
Const InitialValue = 1 ' first sequential number to use
If you wanted to save your files under the names Invoice 1, Invoice 2 as
mentioned above, then you'd change the following line:
Const prefix = "Invoice " ' text to put before the sequential number

Don't be intimidated by Visual Basic because you've never studied it. Even
though you may not understand everything, you should have a basic
understanding of it - then - end if structures. Read the comments, it helps
too.

JL
Mac OS X 10.3.7, Office v.X 10.1.6


W. Kirk Lutz wrote on 2005/01/11 10:59:

I also posted this on MacFixit.com and someone sent this code:

Private Sub Workbook_Open()
Sheet1.[A1] = Sheet1.[A1].Value + 1
End Sub

I changed it to:

Private Sub Workbook_Open()
Sheet1.[E2] = Sheet1.[E2].Value + 1
ThisWorkbook.Save
End Sub

but it doesn't work quite right. It does increase the number in E2 by 1
(from 4401 to 4402) but it doesn't save over the template so the next
time the number is 4403.

Thoughts?

-Kirk


W. Kirk Lutz wrote:



Option Explicit

Private Sub Workbook_Open()
SequentialNumber
End Sub

Sub SequentialNumber()
' JL 2005-01-10
' update 1, 2005-01-11: sequential number now stays correct in Properties
' On each opening of a master file, create a new file named
' with a sequential number

Const InitialValue = 1 ' first sequential number to use
Const increment = 1 ' increment from one sequential number to the next
Const prefix = "" ' text to put before the sequential number
Const suffix = "" ' text to put after the sequential number

Dim existSN As Boolean
Dim existMF As Boolean
Dim master As Boolean
Dim cdp As DocumentProperty
Dim SeqNumber As Long
Dim FSName As Variant

existSN = False
existMF = False
master = False

With ThisWorkbook

For Each cdp In .CustomDocumentProperties
If cdp.Name = "Sequential Number" Then
existSN = True
SeqNumber = cdp + increment
ElseIf cdp.Name = "Master File" Then
existMF = True
master = cdp
End If
Next

' Creation of the custom properties the first time
' the master file is open
' Each property is tested separately in case somebody
' removed a property
If Not existSN Then
.CustomDocumentProperties.Add Name:="Sequential Number", _
LinkToContent:=False, Type:=msoPropertyTypeNumber, _
Value:=InitialValue
SeqNumber = InitialValue
End If
If Not existMF Then
.CustomDocumentProperties.Add Name:="Master File", _
LinkToContent:=False, Type:=msoPropertyTypeBoolean, _
Value:=True
master = True
End If

If master Then ' save a new file only if the master file is open

.CustomDocumentProperties.Item("Sequential Number") = SeqNumber
.Save ' save master file containing the new sequential number

FSName = prefix & Format(SeqNumber) & suffix
.CustomDocumentProperties.Item("Master File") = False

' Two methods for saving the new file.
' Put an apostrophe in front of the unnecessary method's lines
' before use.

' Method 1: new file in same directory than master file
.SaveAs FileName:=.Path & Application.PathSeparator & FSName, _
AddToMru:=True
' end of method 1

' Method 2: Save path requested from user
FSName = Application.GetSaveAsFilename(InitialFilename:=FSName)
If FSName <> False Then
.SaveAs FileName:=FSName, AddToMru:=True
Else
.Saved = True
' to avoid a dialog box if immediately closed
End If
' end of method 2

End If

End With

End Sub
 
W

W. Kirk Lutz

Just re-read my note. To clear it up, all number changes occur to a
cell, not to a document.

-Kirk

W. Kirk Lutz said:
Okay, here is what I would like:

I have a master Excel template stored on our server with a number in a
cell.

When that master Excel template is opened by one of my coworkers(say the
current PO number is 4401), the number in that cell(E2) increases by 1
so that the new files is 4402.

They then save the .xls file anyway they see fit.

The master template .xlt file saves the new number(4402) so that the
next person opening it is greeted with 4403 in cell E2.

-Kirk


J said:
OK, there is one thing that needs to be clarified here.
1) I understood that from a master file (as an example, Invoice
Master), you
wanted to create subsequent files for each invoice you create. Each of
these
files is called for example Invoice 1, Invoice 2, etc. Is that correct?

2) Or do you want that a master file only keeps in a cell a number
going up
each time you open that file? Then you take care of saving the file
yourself
under a new name?

3) Or do you want a combination of both, i.e. a file automatically
bearing a
numbered name plus in one of its cells its own sequential number (like an
invoice would do)?

Now if the case that interests you is 1, then I have an updated
version of
the code posted yesterday, because there was a small flaw concerning
the way
the sequential number was kept in the properties (I know this is above
your
head, but others may be interested). See the new code at the bottom of
the
message. It entirely replaces what I posted yesterday.

If it's case 2, then what you got on MacFixIt was almost right. I assume
that you do a Save As, thus losing the value you want to keep in the
master
file. See in your previous post how to easily fix it.

If it's case 3, then I'd have to make a few easy changes to my code.

Now, the term "Comment out" means to tell the compiler to ignore certain
lines, to treat them like comments instead of computer instructions. The
compiler ignores lines (or parts of lines) that begin with an apostrophe
('). The comments appear green in the code.

The constants are the lines that begin with the word Const. Look for them
near the top of the code. Can you at least make sense of a line that
says:
Const InitialValue = 1 ' first sequential number to use
If you wanted to save your files under the names Invoice 1, Invoice 2 as
mentioned above, then you'd change the following line:
Const prefix = "Invoice " ' text to put before the sequential number

Don't be intimidated by Visual Basic because you've never studied it.
Even
though you may not understand everything, you should have a basic
understanding of it - then - end if structures. Read the comments, it
helps
too.

JL
Mac OS X 10.3.7, Office v.X 10.1.6


W. Kirk Lutz wrote on 2005/01/11 10:59:

I also posted this on MacFixit.com and someone sent this code:

Private Sub Workbook_Open()
Sheet1.[A1] = Sheet1.[A1].Value + 1
End Sub

I changed it to:


Private Sub Workbook_Open()
Sheet1.[E2] = Sheet1.[E2].Value + 1
ThisWorkbook.Save
End Sub

but it doesn't work quite right. It does increase the number in E2 by 1
(from 4401 to 4402) but it doesn't save over the template so the next
time the number is 4403.

Thoughts?

-Kirk


W. Kirk Lutz wrote:


You lost me after pasting the code into the master workbook code.

I don't know how to verify constants or comment out the rest. I don't
know what I'm looking at.

How do I use this? I currently have a cell with 00000 in it. How do I
get this cell to use this script? So the next time the template is
opened the number is 00001?

Sorry, but I have never done anything other than add or multiply
numbers
in Excel. This is way beyond me.

-Kirk


J Laroche wrote:


Bernard Rey wrote on 2005/01/10 17:55:



W. Kirk Lutz wrote:



I am trying to create an Excel master document that when opened,
automatically creates a new version of itself with a number that
grows
with each open.

So if the number starts at 00000 and the master template document is
opened as an Excel sheet opens with that number as 00001

What do I put in the number field for this to happen?



Find the tip on JE's page:

http://www.mcgimpsey.com/excel/udfs/sequentialnums.html


---------------------------------------------------------
Please reply to the newsgroup, and within the same thread.
Merci de répondre au groupe, et dans l'enfilade.





If modifying JE's excellent code (I'm still discovering something:
SaveSetting) to adapt to your situation is out of your or your
friends'
reach, select and copy the code below, and paste it in your master
workbook's code. To access that code, control-click on the workbook's
title
bar and select View Code. Remove the empty Workbook_Open procedure, if
there
is one, before pasting.

Only a few things have to be verified after pasting: the constants
InitialValue, increment, prefix and suffix, and the selection of
saving
method, near the bottom. Comment out the unwanted one. Method 1 will
automatically save the new workbook in the same folder than the master
file,
while method 2 will ask each time where to save (the file can be
renamed at
this point; saving can be cancelled, however the sequential number is
still
increased). That's up to you, but do it before using the master file
for the
first time.

JL
Mac OS X 10.3.7, Office v.X 10.1.6



Option Explicit

Private Sub Workbook_Open()
SequentialNumber
End Sub

Sub SequentialNumber()
' JL 2005-01-10
' update 1, 2005-01-11: sequential number now stays correct in Properties
' On each opening of a master file, create a new file named
' with a sequential number

Const InitialValue = 1 ' first sequential number to use
Const increment = 1 ' increment from one sequential number to the next
Const prefix = "" ' text to put before the sequential number
Const suffix = "" ' text to put after the sequential number

Dim existSN As Boolean
Dim existMF As Boolean
Dim master As Boolean
Dim cdp As DocumentProperty
Dim SeqNumber As Long
Dim FSName As Variant

existSN = False
existMF = False
master = False
With ThisWorkbook
For Each cdp In .CustomDocumentProperties
If cdp.Name = "Sequential Number" Then
existSN = True
SeqNumber = cdp + increment
ElseIf cdp.Name = "Master File" Then
existMF = True
master = cdp
End If
Next

' Creation of the custom properties the first time
' the master file is open
' Each property is tested separately in case somebody
' removed a property
If Not existSN Then
.CustomDocumentProperties.Add Name:="Sequential Number", _
LinkToContent:=False, Type:=msoPropertyTypeNumber, _
Value:=InitialValue
SeqNumber = InitialValue
End If
If Not existMF Then
.CustomDocumentProperties.Add Name:="Master File", _
LinkToContent:=False, Type:=msoPropertyTypeBoolean, _
Value:=True
master = True
End If
If master Then ' save a new file only if the master file
is open
.CustomDocumentProperties.Item("Sequential
Number") = SeqNumber
.Save ' save master file containing the new sequential
number
FSName = prefix & Format(SeqNumber) & suffix
.CustomDocumentProperties.Item("Master File") = False
' Two methods for saving the new file.
' Put an apostrophe in front of the unnecessary method's
lines
' before use.
' Method 1: new file in same directory than
master file
.SaveAs FileName:=.Path & Application.PathSeparator &
FSName, _
AddToMru:=True
' end of method 1
' Method 2: Save path requested from user
FSName =
Application.GetSaveAsFilename(InitialFilename:=FSName)
If FSName <> False Then
.SaveAs FileName:=FSName, AddToMru:=True
Else
.Saved = True
' to avoid a dialog box if immediately closed
End If
' end of method 2
End If
End With
End Sub
 
J

J Laroche

** QUICK NOTE TO THOSE WHO COPIED THE CODE ALREADY: a new version appears at
the bottom of this message **

Well, well, well. Your master is a template? It makes sense, normally, but
what you want cannot work with a template. It's because when you open a
template it immediately becomes a document with a different name, so saving
the template back with a new number inside is not so easy. You manually have
to take extra steps. That's why your number doesn't go up after 4402. But if
you open an already saved PO, its number will increase again, which is
probably not what you want either. And my code, posted twice already,
doesn't work at all when it's inside a template file.

If you insist on keeping the file as a template you have no choice but adapt
the code offered on JE McGimpsey's web site and use an external text file to
keep the number. On the other hand, if you use this third version of my code
you MUST save the workbook as a standard Excel workbook. But have no fear,
with the code it mostly behaves like a template, in the sense that the
original is never modified, except for the running number.

One final note: for the sake of universality, I didn't use a fixed address
for your PO number. Instead I used a range called PONumber. So you have to
create that name somewhere on any sheet (most likely E2 on sheet 1 in your
case). To create the name select the cell to be named; click in the Name Box
at the left of the formula bar, type PONumber, and press Enter. Then replace
the VBA code already in the file by the one at the bottom. Modify the four
constants if necessary. Remove saving method 2 if you don't want the
salespersons to be greeted with a Save dialog box each time the master is
open. Finally, save the file with an appropriate name for a master. That
should do it.

JL
Mac OS X 10.3.7, Office v.X 10.1.6


W. Kirk Lutz wrote on 2005/01/12 10:20:
Okay, here is what I would like:

I have a master Excel template stored on our server with a number in a cell.

When that master Excel template is opened by one of my coworkers(say the
current PO number is 4401), the number in that cell(E2) increases by 1
so that the new files is 4402.

They then save the .xls file anyway they see fit.

The master template .xlt file saves the new number(4402) so that the
next person opening it is greeted with 4403 in cell E2.

-Kirk


J said:
OK, there is one thing that needs to be clarified here.
1) I understood that from a master file (as an example, Invoice Master), you
wanted to create subsequent files for each invoice you create. Each of these
files is called for example Invoice 1, Invoice 2, etc. Is that correct?

2) Or do you want that a master file only keeps in a cell a number going up
each time you open that file? Then you take care of saving the file yourself
under a new name?

3) Or do you want a combination of both, i.e. a file automatically bearing a
numbered name plus in one of its cells its own sequential number (like an
invoice would do)?

Now if the case that interests you is 1, then I have an updated version of
the code posted yesterday, because there was a small flaw concerning the way
the sequential number was kept in the properties (I know this is above your
head, but others may be interested). See the new code at the bottom of the
message. It entirely replaces what I posted yesterday.

If it's case 2, then what you got on MacFixIt was almost right. I assume
that you do a Save As, thus losing the value you want to keep in the master
file. See in your previous post how to easily fix it.

If it's case 3, then I'd have to make a few easy changes to my code.

Now, the term "Comment out" means to tell the compiler to ignore certain
lines, to treat them like comments instead of computer instructions. The
compiler ignores lines (or parts of lines) that begin with an apostrophe
('). The comments appear green in the code.

The constants are the lines that begin with the word Const. Look for them
near the top of the code. Can you at least make sense of a line that says:
Const InitialValue = 1 ' first sequential number to use
If you wanted to save your files under the names Invoice 1, Invoice 2 as
mentioned above, then you'd change the following line:
Const prefix = "Invoice " ' text to put before the sequential number

Don't be intimidated by Visual Basic because you've never studied it. Even
though you may not understand everything, you should have a basic
understanding of it - then - end if structures. Read the comments, it helps
too.

JL
Mac OS X 10.3.7, Office v.X 10.1.6


W. Kirk Lutz wrote on 2005/01/11 10:59:

I also posted this on MacFixit.com and someone sent this code:

Private Sub Workbook_Open()
Sheet1.[A1] = Sheet1.[A1].Value + 1
End Sub

I changed it to:

Private Sub Workbook_Open()
Sheet1.[E2] = Sheet1.[E2].Value + 1
ThisWorkbook.Save
End Sub

but it doesn't work quite right. It does increase the number in E2 by 1
(from 4401 to 4402) but it doesn't save over the template so the next
time the number is 4403.

Thoughts?

-Kirk


W. Kirk Lutz wrote:


You lost me after pasting the code into the master workbook code.

I don't know how to verify constants or comment out the rest. I don't
know what I'm looking at.

How do I use this? I currently have a cell with 00000 in it. How do I
get this cell to use this script? So the next time the template is
opened the number is 00001?

Sorry, but I have never done anything other than add or multiply numbers
in Excel. This is way beyond me.

-Kirk


J Laroche wrote:


Bernard Rey wrote on 2005/01/10 17:55:



W. Kirk Lutz wrote:



I am trying to create an Excel master document that when opened,
automatically creates a new version of itself with a number that grows
with each open.

So if the number starts at 00000 and the master template document is
opened as an Excel sheet opens with that number as 00001

What do I put in the number field for this to happen?


Find the tip on JE's page:

http://www.mcgimpsey.com/excel/udfs/sequentialnums.html


---------------------------------------------------------
Please reply to the newsgroup, and within the same thread.
Merci de répondre au groupe, et dans l'enfilade.




If modifying JE's excellent code (I'm still discovering something:
SaveSetting) to adapt to your situation is out of your or your friends'
reach, select and copy the code below, and paste it in your master
workbook's code. To access that code, control-click on the workbook's
title
bar and select View Code. Remove the empty Workbook_Open procedure, if
there
is one, before pasting.

Only a few things have to be verified after pasting: the constants
InitialValue, increment, prefix and suffix, and the selection of saving
method, near the bottom. Comment out the unwanted one. Method 1 will
automatically save the new workbook in the same folder than the master
file,
while method 2 will ask each time where to save (the file can be
renamed at
this point; saving can be cancelled, however the sequential number is
still
increased). That's up to you, but do it before using the master file
for the
first time.

JL
Mac OS X 10.3.7, Office v.X 10.1.6

Option Explicit

Private Sub Workbook_Open()
SequentialNumber
End Sub

Sub SequentialNumber()
' JL 2005-01-10
' update 1, 2005-01-11: sequential number now stays correct in Properties
' update 2, 2005-10-12: a range called PONumber is also getting the
' sequential number. The user must create one in the workbook.

' On each opening of a master file, create a new file named
' with a sequential number

Const InitialValue = 1 ' first sequential number to use
Const increment = 1 ' increment from one sequential number to the next
Const prefix = "" ' text to put before the seq. number in the file's name
Const suffix = "" ' text to put after the seq. number in the file's name

Dim existSN As Boolean
Dim existMF As Boolean
Dim master As Boolean
Dim cdp As DocumentProperty
Dim SeqNumber As Long
Dim FSName As Variant

existSN = False
existMF = False
master = False

With ThisWorkbook

For Each cdp In .CustomDocumentProperties
If cdp.Name = "Sequential Number" Then
existSN = True
SeqNumber = cdp + increment
ElseIf cdp.Name = "Master File" Then
existMF = True
master = cdp
End If
Next

' Creation of the custom properties the first time the
' master file is open
' Each property is tested separately in case somebody
' removed a property
If Not existSN Then
.CustomDocumentProperties.Add Name:="Sequential Number", _
LinkToContent:=False, Type:=msoPropertyTypeNumber, _
Value:=InitialValue
SeqNumber = InitialValue
End If
If Not existMF Then
.CustomDocumentProperties.Add Name:="Master File", _
LinkToContent:=False, Type:=msoPropertyTypeBoolean, _
Value:=True
master = True
End If

If master Then ' save a new file only if the master file is open

On Error Resume Next
Range("PONumber").Value = SeqNumber
On Error GoTo 0

.CustomDocumentProperties.Item("Sequential Number") = SeqNumber
' save the master file containing the new sequential number
.Save

FSName = prefix & Format(SeqNumber) & suffix
.CustomDocumentProperties.Item("Master File") = False

' Two methods for saving the new file.
' Put an apostrophe in front of the unnecessary method's
' lines before use.

' Method 1: new file in same directory than master file
'.SaveAs FileName:=.Path & Application.PathSeparator & FSName, _
AddToMru:=True
' end of method 1

' Method 2: Save path requested from user
FSName = Application.GetSaveAsFilename(InitialFilename:=FSName)
If FSName <> False Then
.SaveAs FileName:=FSName, AddToMru:=True
Else
' to avoid a dialog box if immediately closed
.Saved = True
End If
' end of method 2

End If

End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top