You are here:

Excel/Copy hidden sheet & rename

Advertisement


Question
Hi Tom,

I’m not sure if this can be done, but this is what I would like to do in VBA.

I am going to have several worksheets, the first one holds some source data that the others feed from, it is called “NAMES” and is the first sheet (always to the left).

The other sheet/s are called “Country 1”, “Country 2”…….“Country 18” and so on and these are going to be added as needed, so what I am wanting to do is have a hidden sheet (so it cannot be seen or altered) that is formatted how I want with formulas etc, have it named say “Spare” and then when a new sheet needs to be added the “Spare sheet can be copied and given a new name. The new name would be the highest “Country” so if “Country 5” was the current highest sheet the newly copied “Spare” sheet would be then called “Country 6” this would be then placed just after the “NAMES” sheet or the 2nd  sheet from the left.

I hope this can be achieved and if you need any further information that I may have omitted please let me know.

Thanks you very much for your assistance.

Regards
Mike

Answer
Mike,

Here is some starter code:  

Sub abc()
Dim sh1 As Worksheet
Dim s As String, l As Long, maxval As Long, sname As String
Dim sh As Worksheet
Set sh = Worksheets("Spare")
sh.Visible = xlSheetVisible
sh.Copy After:=Worksheets("Names")
Set sh1 = ActiveSheet
sh.Visible = xlSheetHidden
maxval = 0
For Each sh In Worksheets
If sh.Visible Then
  sname = sh.Name
  If InStr(1, sname, "country", vbTextCompare) Then
     s = Mid(sname, 9, 2)
     l = CLng(s)
     If maxval < l Then maxval = l
  End If
End If
Next
sh1.Name = "Country " & maxval + 1
End Sub


--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.