autonumbering a template (.xlt)

mplees

Active Member
Joined
Feb 6, 2006
Messages
351
Hi,

I've been trying to find a way to auto number a workbook that is created from a template (.xlt). I've tried various bits of code, but nothing seems to work quite right. After browsing the forum archive, I first tried this code:

Private Sub Workbook_Open()
Dim nmbr As Long
Dim fNum As Integer
fNum = FreeFile
Open ThisWorkbook.Path & "\" & "lastvalue.txt" For Random As #fNum Len = Len(nmbr)

Get #fNum, 1, nmbr
nmbr = nmbr + 1
Put #fNum, 1, nmbr
Close #fNum
If Sheets("Start").Range("F14").Value = "" Then Sheets("Start").Range("F14").Value = "MSR" & nmbr + 50

End Sub

...which works fine with a workbook, but when I try it with the template, I cannot find the lastvalue.txt file anywhere, & the number does not always increment. I then tried a different approach:

Private Sub Workbook_Open()

Application.DisplayAlerts = False

Sheets("Start").Range("AA1").Value = Sheets("Start").Range("AA1").Value + 1
ThisWorkbook.SaveAs Filename:=Application.ActiveWorkbook.Path & "\" & "tst xxx machine study dd mmm yyyy.xlt"

Application.DisplayAlerts = True

End Sub

...which seems to work the first time I run it, except that when the .xlt file is saved, it doesn't overwrite the original template. & also, the workbook path is displayed as a null string. Again, this works fine if I use a workbook instead of a template.

Storing the incremental number within the template would be my preferred method, so can anyone point me in the right direction with this?

Regards,

Mark
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Mark.
Use the registry if the file will stay on the same system. Run "TempToSetInitialValue" to set the intitial value of your number or write more sophisticated code to prompt the user to do so...

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_Open()
       <font color="#0000A0">Dim</font> nmbr <font color="#0000A0">As</font> <font color="#0000A0">Long</font>

       nmbr = GetSetting("MSR_NUMBER", "MSR_NUMBER", "MSR_NUMBER", 0)
       <font color="#0000A0">If</font> Sheets("Start").Range("F14").Value = "" <font color="#0000A0">Then</font>
           Sheets("Start").Range("F14").Value = "MSR" & nmbr + 50
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
       SaveSetting "MSR_NUMBER", "MSR_NUMBER", "MSR_NUMBER", nmbr + 1
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Sub</font> TempToSetInitialValue()
       <font color="#0000A0">Dim</font> CorrectValue <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
      <font color="#008000"> 'change the zero to the current correct number</font>
       CorrectValue = 0
       SaveSetting "MSR_NUMBER", "MSR_NUMBER", "MSR_NUMBER", CorrectValue
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("9420063838187").value=document.all("9420063838187").value.replace(/<br \/>\s\s/g,"");document.all("9420063838187").value=document.all("9420063838187").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("9420063838187").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="9420063838187" wrap="virtual">
Private Sub Workbook_Open()
Dim nmbr As Long

nmbr = GetSetting("MSR_NUMBER", "MSR_NUMBER", "MSR_NUMBER", 0)
If Sheets("Start").Range("F14").Value = "" Then
Sheets("Start").Range("F14").Value = "MSR" & nmbr + 50
End If
SaveSetting "MSR_NUMBER", "MSR_NUMBER", "MSR_NUMBER", nmbr + 1
End Sub

Sub TempToSetInitialValue()
Dim CorrectValue As Long
'change the zero to the current correct number
CorrectValue = 0
SaveSetting "MSR_NUMBER", "MSR_NUMBER", "MSR_NUMBER", CorrectValue
End Sub</textarea>
 
Upvote 0
Hi Tom,

Many thanks for your quick reply. Your code does the job brilliantly - many thanks for your help!

Regards,

Mark
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

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