简体   繁体   中英

Define worksheet name depends on variable

For i = 1 To n
    Sheets(2).Copy After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = "SheetName(i)"
Next

Here I want to define sheetName depends on variable "i". For example, when i = 1, name should be SheetName1 . Can u help me?

Instead of "SheetName(i)" use "SheetName" & i" , try this:

Sub Demo()
    For i = 1 To n
        Sheets(2).Copy After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = "SheetName" & i
    Next i
End Sub

This is a good way to do it:

Option Explicit

Public Sub TestMe()
    Dim i As Long
    Dim n As Long: n = Worksheets.Count

    For i = 1 To n
        Worksheets(i).Copy After:=Worksheets(Worksheets.Count)
        Worksheets(Worksheets.Count).name = "SheetName" & i
    Next i
End Sub

What I have used:

  • You are speaking about Worksheets , but you are using Sheets . There is a difference between these two. Sheets include Worksheets and Charts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM