format number in VBA as 001

gellis11239

New Member
Joined
Dec 23, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi all, i'm sure i'm missing something easy.

I have some code that copies a sheet and renames it in numerical order. (First sheet is "PON 9001") but the way this code is the current way I get PON 91, PON 92 etc. I really need PON 9001, PON 9002....... al the way to PON 9500. I just can't work it out.

Current code is
VBA Code:
Sub Create()
    Dim I As Long
    Dim xNumber As Integer
    Dim xName As String
    Dim xActiveSheet As Worksheet
    On Error Resume Next
    Application.ScreenUpdating = False
    Set xActiveSheet = ActiveSheet
    xNumber = InputBox("Enter number of times to copy the current sheet")
    For I = 1 To xNumber
        xName = ActiveSheet.Name
        xActiveSheet.Copy After:=ActiveWorkbook.Sheets(xName)
        ActiveSheet.Name = "PON 9" & I
        
    Next
    xActiveSheet.Activate
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,

use

VBA Code:
ActiveSheet.Name = "PON " & 9000 + I
 
Upvote 0
Hi,
welcome to forum

try this update to your code & see if does what you want

VBA Code:
Sub Create()
    Dim i               As Long
    Dim xNumber         As Variant
    Dim xName           As String, SheetName As String
    Dim xActiveSheet    As Worksheet
   
    Set xActiveSheet = ActiveSheet
   
    Do
        xNumber = InputBox("Enter number of times To copy the current sheet")
        'cancel pressed
        If StrPtr(xNumber) = 0 Then Exit Sub
    Loop Until IsNumeric(xNumber)
   
    Application.ScreenUpdating = False
    For i = 1 To xNumber
        SheetName = "PON 9" & Format(i, "000")
        'check sheet exists
        If Not Evaluate("ISREF('" & SheetName & "'!A1)") Then
            'xName = ActiveSheet.Name
            xActiveSheet.Copy After:=ActiveWorkbook.Sheets(Sheets.Count)
            ActiveSheet.Name = SheetName
        End If
    Next
    xActiveSheet.Activate
    Application.ScreenUpdating = True
End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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