Determine sheet number of new added sheet but InternalName alerady exists

Posted on 2011-10-28
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

    LVL 92

    Accepted Solution

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
    This collection of functions covers all the normal rounding methods of just about any numeric value.
    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…
    This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now