Excel

Autonumber Cell Value Increase by One on Open

Ease of Use

Easy

Version tested with

2000 

Submitted by:

lucas

Description:

Each time the file is opened, the designated cell value is increased by one, whether you save the file or not. 

Discussion:

if the Excel file is classified as "Read Only" the counter will still work! The other thing about it that's neat is that it saves a sequential text file to a local user directory. This allows a network administrator to keep track of how many times individuals open a single file, adding up all the individual numbers of course gives the total file usage for the network. Uses a textfile to hold the value. Location for the textfile can be changed in the code. (Some code by Aaron Blood at www.XL-Logic.com formed the basis for this code) 

Code:

instructions for use

			

Option Explicit Private Sub Workbook_Open() Dim x As String On Error GoTo ErrorHandler One: Open "c:\" & ThisWorkbook.Name & _ " Counter.txt" For Input As #1 Input #1, x Close #1 x = x + 1 Two: '******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

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Tools/Project Explorer
  4. Select the ThisWorkbook module
  5. Copy and paste the code into this module
  6. Edit the location of where you want the Counter text file to reside or use the default.
  7. Now select File/Close and Return To Microsoft Excel
  8. Dont forget to save your changes...
 

Test the code:

  1. Open the workbook, enter a number in the input box for the start of the count
  2. Close and re-open the workbook, the number you gave will be incremented by one.
  3. (It automatically increments by 1 each time it is opened now, whether you save or not).
  4. You can also change (or even omit) the cell where the value is reflected in the code. (It is set to be displayed in cell A1 right now).
  5. Look in your C drive for the text file 'count_seq_file.xls Counter' and open it to view the count.
  6. NOTE: The code creates the text file 'count_seq_file.xls Counter'
  7. You can edit the textfile at any time to change the count.
 

Sample File:

count_seq_file.zip 8.17KB 

Approved by mdmackillop


This entry has been viewed 198 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express