Counting and Greater than actions??

KentKHI

Active Member
Joined
Oct 1, 2004
Messages
492
I have an issue where I am naming sheets in code with a prefix such as A or CO or OPT, and a suffix that is a number. Currently it automatically assigns a number using the following code:
Code:
    ShCount = 0
    Duh = UserForm1.ComboBox1.Value
    Criterion = Duh + "*"
    If Criterion = "" Then Exit Sub
    For Each sh In Sheets
    If sh.Name Like Criterion Then ShCount = ShCount + 1
    Next sh
where Shcount is an integer, and sh is an object, sheet.

The problem is if I delete a sheet in the middle of a group (example: A1 A2 A3 A5 A6 A7) I get an error because I am trying to name a sheet the same as an existing sheet.

Is there a way to find the highest integer and +1 in order to not name sheets the same name? (result should be: A8)

(I know there is a way, but how?)

Also, or otherwise, even better, is there a way to name it using the first missing positive integer? (result should be: A4)

Thanks for any help on this!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
The problem is if I delete a sheet in the middle of a group (example: A1 A2 A3 A5 A6 A7) I get an error because I am trying to name a sheet the same as an existing sheet.

Oops
I get the error when I add a new sheet!
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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