Auto-Generate Unique ID Number

nrb4480

Board Regular
Joined
Jun 19, 2008
Messages
69
I have the following code that inputs a unique ID number everytime a workbook is opened. Instead of inserting a new number everytime the workbook is opened, I want to also insert a new number everytime the sheet is cleared (I currently have a clear all values button on the worksheet). Any ideas on how to modify the code to work from the clear command button also?


Option Explicit

Private Sub Workbook_Open()

Dim x As String

On Error Goto ErrorHandler
Open "c:\" & ThisWorkbook.Name & _
" Counter.txt" For Input As #1
Input #1, x
Close #1
x = x + 1
'******THIS LINE IS OPTIONAL******
Sheets(1).Range("A1").Value = x
'********************************
Open "c:\" & ThisWorkbook.Name & _
" Counter.txt" For Output As #1
Write #1, x
Close #1

Exit Sub

ErrorHandler:
Select Case Err.Number

Case 53 'If Counter file does not exist...
NumberRequired:
x = InputBox("Enter a Number greater than " & _
"zero to Begin Counting With", _
"Create 'C:\" & ThisWorkbook.Name & _
" Counter.txt' File")
If Not IsNumeric(x) Then Goto NumberRequired
If x <= 0 Then Goto NumberRequired
Resume Two
Case Else
Resume Next
End Select
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,037
Messages
5,856,966
Members
431,841
Latest member
jaybeem

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
Top