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
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