Issue incrementing a variable

MrFlooglebinder

New Member
Joined
Feb 19, 2015
Messages
3
Hi Guys,
I am hoping someone can pin point what I am doing wrong here.


I am trying to auto generate 31 sheets, each sheet is named numerically from 1-31
My issue is that every 10th sheet gives me a number that is not correct.
The sheets end up getting labeled like the following. (sheets separated by commas)
1,2,3,4,5,6,7,8,9,143043,11,12,13,14,15,16,17,18,19,243043,21,21....etc


Note: before I run the code I already have a sheet labeled "1" which has a template on it that is copied to each new sheet that gets generated.


Code:
Sub Macro1()
Dim sheetname As String
sheetname = 2
    For i = 2 To 31
        Sheets("1").Select
        Cells.Select
        Selection.Copy
        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = WorksheetFunction.Text(Now(), sheetname)
        ActiveSheet.Paste
        sheetname = sheetname + 1
    Next i
Sheets("1").Select
Range("V3").Select
    
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi ,

Try this :
Code:
Sub Macro1()
    Dim sheetname As Integer
    sheetname = 2
    For i = 2 To 31
        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = WorksheetFunction.Text(sheetname, "0")
        Sheets(1).Cells.Copy ActiveSheet.Range("A1")
        sheetname = sheetname + 1
    Next i
    Sheets(1).Select
    Range("V3").Select
End Sub
 
Upvote 0
Hey Thanks for your reply,
Your code worked a treat for generating the numbered sheets, but it messed a couple of other things up in the process.
However, you provided exactly what I needed so was able to tweak it and the below is the result which works great.
Thanks for your help!

Code:
Sub Macro1()
    Dim sheetname As Integer
    sheetname = 2
    For i = 2 To 31
        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = WorksheetFunction.Text(sheetname, "0")
        Sheets("1").Cells.Copy ActiveSheet.Range("A1")
        sheetname = sheetname + 1
    Next i
    Sheets("1").Select
    Range("V3").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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