VBA Add 0 in front of a Single-digit Number

Apple1

Board Regular
Joined
Jan 18, 2015
Messages
121
Hi,

Using Excel 2010. I have the below macro to create new sheets. I want my MTH which is part of each sheet name to have a "0" in front if it is single digit.

MTH increases by 1 with each iteration.

If MTH <10, it should have a 0 in front.
IF MTH>=10, no need to have a 0 in front.

Code:
Sub GenSheets1()

Dim i As Integer
Dim Mth As String

Mth = "05"

Application.ScreenUpdating = False


        For i = 1 To 3
            Worksheets("Sheet1").Copy after:=Sheets(Sheets.Count)
            Mth = Mth + 1
            ActiveSheet.Name = "Book" & Mth
        Next

        Application.ScreenUpdating = True

End Sub

Thank you.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this change to your code & see if does what you want:

Code:
Sub GenSheets1()


    Dim i As Integer
    Dim Mth As Integer
    
    Mth = 5
    
    Application.ScreenUpdating = False




    For i = 1 To 3
        Worksheets("Sheet1").Copy after:=Sheets(Sheets.Count)
        Mth = Mth + 1
        ActiveSheet.Name = "Book" & Format(Mth, "00")
    Next


    Application.ScreenUpdating = True


End Sub

Dave
 
Upvote 0
Sub GenSheets1()


Dim i As Integer
Dim Mth As String


Mth = "08"


Application.ScreenUpdating = False




For i = 1 To 3
ThisWorkbook.Worksheets("Sheet1").Copy after:=Sheets(Sheets.Count)
Mth = Mth + 1
If Mth < 10 Then
Mth = "0" & Mth
End If
ActiveSheet.Name = "Book" & Mth
Next


Application.ScreenUpdating = True


End Sub



Hi,

Using Excel 2010. I have the below macro to create new sheets. I want my MTH which is part of each sheet name to have a "0" in front if it is single digit.

MTH increases by 1 with each iteration.

If MTH <10, it should have a 0 in front.
IF MTH>=10, no need to have a 0 in front.

Code:
Sub GenSheets1()

Dim i As Integer
Dim Mth As String

Mth = "05"

Application.ScreenUpdating = False


        For i = 1 To 3
            Worksheets("Sheet1").Copy after:=Sheets(Sheets.Count)
            Mth = Mth + 1
            ActiveSheet.Name = "Book" & Mth
        Next

        Application.ScreenUpdating = True

End Sub

Thank you.
 
Upvote 0
Dear Apple
Hope below code helps, Mth could be declared as any value in the beginning as per your requirement.

Sub GenSheets1()


Dim i As Integer
Dim Mth As String


Mth = "08"


Application.ScreenUpdating = False




For i = 1 To 3
ThisWorkbook.Worksheets("Sheet1").Copy after:=Sheets(Sheets.Count)
Mth = Mth + 1
If Mth < 10 Then
Mth = "0" & Mth
End If
ActiveSheet.Name = "Book" & Mth
Next


Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,923
Members
449,479
Latest member
nana abanyin

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