Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Determine sheet number of new added sheet but InternalName alerady exists

Posted on 2011-10-28
3
Medium Priority
?
300 Views
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
    Sheets("CurrentVolumes").Select
    Columns("A:K").Select
    Selection.Copy
    Sheets("strTabName").Select
    Range("A1").Select
    ActiveSheet.Paste
End Sub

Open in new window

0
Comment
Question by:ssmith94015
3 Comments
 
LVL 10

Expert Comment

by:etech0
ID: 37045583
sheets.count
0
 
LVL 93

Accepted Solution

by:
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

0
 

Author Closing Comment

by:ssmith94015
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.
0

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