![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
|
i have some code for renaming sheets which works fine until you try to do the same thing twice in a row for eg. Below
Sheets("111stk").Select ActiveWindow.SelectedSheets.Delete Sheets.Add Sheets("Sheet1").Name = "111stk" Sheets("211stk").Select ActiveWindow.SelectedSheets.Delete Sheets.Add Sheets("Sheet2").Name = "211stk" Range("G34").Select Sheets("511stk").Select ActiveWindow.SelectedSheets.Delete Sheets.Add Sheets("Sheet3").Name = "511stk" has anyone got any better code so I don't have to refer to "Sheet3" and so on HELp? |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Hi BrettVBA
Looking @ your current code I would say that you are currenly adding new sheets over the old ones ie. deleting some currently setup sheets then adding new ones Named as the same. What you have found out is that the sheets counter increments in the name. So that referencing the sheet as sheet3 etc is not valid after a 2nd call. If your sheets names are cost as you have set up then try this Option Explicit Option Base 1 Sub AddSheets() Dim aSheets() Dim i As Integer aSheets = Array("111stk", "211stk", "511stk") Application.DisplayAlerts = False On Error GoTo ErrAdd For i = 1 To 3 Sheets(aSheets(i)).Delete Sheets.Add.Name() = aSheets(i) Next Exit Sub ErrAdd: MsgBox Err.Number & ":=" & Err.Description, vbSystemModal + vbMsgBoxHelpButton, _ "Error Adding Sheet", Err.HelpFile, Err.HelpContext End Sub |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
|
|
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Christchurch New Zealand
Posts: 1,030
|
thats exactually what I needed thanks Ivan
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|