Option Explicit
Sub testme()
Dim iCtr As Long
Dim wks As Worksheet
Dim myStr As String
Set wks = Worksheets.Add
iCtr = -1
Do
iCtr = iCtr + 1
If iCtr = 0 Then
myStr = ""
Else
myStr = Format(iCtr, "0")
End If
On Error Resume Next
wks.Name = "Database" & myStr
If Err.Number = 0 Then
'it worked
Exit Do
Else
Err.Clear
'keep looking, stay in loop
End If
Loop
MsgBox wks.Name
End Sub
You may want to consider naming your sheets Database001, database002, ... If
you ever decide to sort those sheets, they'll be easy to sort.
This line:
myStr = Format(iCtr, "0")
would become
myStr = Format(iCtr, "000")
And if you ever want to sort the sheets...
Chip Pearson's code:
http://www.cpearson.com/excel/sortws.htm
David McRitchie's code:
http://www.mvps.org/dmcritchie/excel...#sortallsheets
Jac wrote:
hi,
I have created a MS Excel macro that can help me to insert a new sheet and
named it as Database but this macro will delete the Database sheet if I run
it again.
So, I would like to create a MS Excel macro which could help me to name each
sheet in series. For example, if previously there is a sheet already named as
Database1, then the next time, I run the macro it should name the newly
inserted sheet as Database2.
Could anyone help? cause I have no ideal how to compare the names of sheets
in a workbook.
Thanking in advance
--
Dave Peterson