counter on external file

fddekker

Board Regular
Joined
Jun 30, 2008
Messages
86
Office Version
  1. 365
Platform
  1. Windows
I have the following scenario:
<o:p> </o:p>
STEP 1
Users open a template, complete the template, save the file as an .xls file and then e-mail it to me.
<o:p> </o:p>
STEP 2
When I receive the e-mail, I open the e-mailed file, check the entries and then run a macro from an add-in I wrote on the file that digitally signs the file and locks all cells. I then e-mail the file to a processing department. (In my absence, a co-worker with access to the add-in validates the coding)
<o:p> </o:p>
Needed.
The processing department would like to have each validated file numbered with a sequential number, let’s say starting with 10010001 (next number 10010002 etc.)
Is there a way that I can have the macro access a third file, pick up the last number, increase it by one and paste it in let’s say cell D3 of the file?
(Since I would like only validated files numbered, I don’t want to save the counter with the template).
<o:p> </o:p>
Any help would be appreciated.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Saving and getting from the registry is one method.

Here is a txt file method.
Code:
Sub Test()
 Dim s As String, count As Long
 s = ThisWorkbook.Path & "\Counter.txt"
 count = ReadCounter(s)
 MsgBox "Current count=" & count
 IncCounter (s)
 count = ReadCounter(s)
 MsgBox "LastCount+1=" & count
End Sub

Sub IncCounter(toFile As String)
  Dim iHandle As Integer, counter As Long
  On Error Resume Next
  counter = ReadCounter(toFile)
  iHandle = FreeFile
  Open toFile For Output Access Write As #iHandle
  Print #iHandle, counter + 1
  Close #iHandle
End Sub

Function ReadCounter(filePath As String) As Long
  Dim str As String, hFile As Integer
  On Error Resume Next
  hFile = FreeFile
  Open filePath For Binary Access Read As #hFile
  str = Input(LOF(hFile), hFile)
  ReadCounter = str
  Close hFile
End Function
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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