Naming worksheets in loop

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi! I am writing a macro for excel that generates new worksheets. I want to
name the worksheets so that all worksheets has one part of their name that is
the same for all worksheets and I want to have one variable part. My erronous
code now is:

Set NewSheet = Worksheets.Add
NewSheet.Name = "My worksheet" + x

I want to have a counter for x but I do not know how to comine the String
part ("My worksheet") with the value of the variabel so that the final name
is e.g. My worksheet 14. Help please!
 
Hi Jenni,

Try:

'=============>>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim iCtr As Long
Const sName As String = "MyWorksheet" '<<==== CHANGE

Set WB = Workbooks("YourBook.xls") '<<==== CHANGE

iCtr = WB.Worksheets.Count

Set SH = Worksheets.Add(after:=Worksheets(iCtr))

SH.Name = sName & iCtr

End Sub
'<<=============
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top