Counter Macro Problem

razor22

New Member
Joined
Jul 28, 2011
Messages
6
I am trying to use a simple macro that will add "1" to my counter each time the worksheet is saved. I can only figure out to add 1 when I keep the number in simple context (ie 1,2,3,4,5 ....) I am using this:

Private Sub Workbook_Open()
Sheets("Sheet1").Range("K4") = Val(Sheets("Sheet1").Range("K4")) + 1
End Sub

I need for the counter to take my form number (HA000) and each time it is saved , to add 1 (ie HA001, HA002, HA003, HA004)

Thank you for your help in advance. If you need any further clarification, I will be online.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to the Board!

Try this variation:
Code:
Sheets("Sheet1").Range("K4") = Left(Sheets("Sheet1").Range("K4"), 2) & Format(Right(Sheets("Sheet1").Range("K4"), 3) + 1, "000")
 
Upvote 0
Something like this maybe:

Code:
Private Sub Workbook_Open()
With Sheets("Sheet1").Range("K4")
.Value = "HA" & RIGHT(.Value,3) + 1
End With
End Sub
 
Upvote 0
Starting to work ....

Private Sub Workbook_Open()
With Sheets("Sheet1").Range("K4")
.Value = "HA" & RIGHT(.Value,3) + 1
End With
End Sub

However it is saving now as "HA1,HA2,HA3"

I would really like for it to display "HA001,HA002,HA003"

Once again, thank you!
 
Upvote 0
I am trying to use a simple macro that will add "1" to my counter each time the worksheet is saved
Another note. You have selected the Workbook_Open event. That increments the counter every time the workbook is opened. Obviously, that incrementer won't get saved unless the save the workbook, so that should work in your scenario. But just also be aware that there is a "Workbook_BeforeSave" event procedure also, which could be used.

The big difference is, with the "Workbook_BeforeSave" event procedure, it will get updated every time the file is saved. So if people hit save multiple times in a single session, it will increment the counter that many times. Whereas if it is in the "Workbook_Open" event, it will only increment once, per time that it is opened, and if nothing is saved, the incremented value will not be saved either.

Just important to be aware of the different options so you can pick the one that works best for you.
 
Upvote 0
Joe 4:

Thanks for the welcome. Your variation has worked flawlessly. I really appreciate everyone help. I will be on these threads all day now!
 
Upvote 0
njimack's solution will also work with one minor edit. Just need to add the FORMAT function to it, i.e.
Code:
Private Sub Workbook_Open()
    With Sheets("Sheet1").Range("K4")
        .Value = "HA" & Format(Right(.Value, 3) + 1, "000")
    End With
End Sub
With my solution, you can also hard-code the first part as "HA" if you like. I chose to pull the existing two character value from the existing value. This makes it a little more dynamic so it will work with any two character prefix without having to update the code.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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