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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,

use

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

Try
VBA Code:
 ActiveSheet.Name = "PON 9" & "00" & I
That would give results like PON 90010 and PON 900500 rather than PON 9010 and PON 9500.
 
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,215,314
Messages
6,124,202
Members
449,147
Latest member
sweetkt327

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