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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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