Learn how to a build a cloud-first strategyRegister Now


Determine sheet number of new added sheet but InternalName alerady exists

Posted on 2011-10-28
Medium Priority
Last Modified: 2012-05-12
I have a process that needs to take data from one worksheet to another.  The secnod worksheet is created in code and renamed.  However, the sheet name can be anything like "Sheet#".  However, since the worksheet that is added can be any sheet number, how do I determine what the sheet number is so I can refer to it in code?  That is, the new sheet's internal name can be say, Sheet16, but its tab name woudl be Sheet9 - which can be the internal name of an existing worksheet.  So, I need to determine the internal sheet name of the newly added worksheet.
Public Sub CreateWorksheet(strTabName)
'Each month creates a new worksheet
Dim strSheetName    As String
strSheetName = strTabName
    Sheets.Add After:=Sheets("CurrentVolumes")
    Sheets("Sheet8").Name = strTabName
End Sub

Open in new window

Question by:ssmith94015
LVL 10

Expert Comment

ID: 37045583
LVL 93

Accepted Solution

Patrick Matthews earned 2000 total points
ID: 37045587
Use a variable to track it:

Public Sub CreateWorksheet(strTabName)
'Each month creates a new worksheet
Dim strSheetName    As String, Ws As Worksheet

strSheetName = strTabName
Set Ws = Worksheets.Add(After:=Worksheets("CurrentVolumes"))
    Ws.Name = strTabName
    Worksheets("CurrentVolumes").Range("A:K").Copy Ws.Range("A1")
End Sub

Open in new window


Author Closing Comment

ID: 37045649
Sheets count won't work as I tried that, if the user adds and delete sheets during the process, the count will be off.  Matthews, this does the trick, thank you both.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question