Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help

Status
Not open for further replies.

dd192193

Technical User
Jan 7, 2004
2
NL
record a macro wich delete data from several sheets those sheets names change all the time how can i make it happen that when i change the sheet name it will also be changed in the visual basic code at this time i got the error message runtime error'9': subschript out of range .
working with one workbook and about 20 sheets with different names and contiquous changing .(excel)

danny
 
Can't understand why you keep changing the sheet names - that must get confusing??
Anyway, assuming that the sheets only change names, but do not get created or destroyed, then you can refer to the sheets by ordinal number instead of name.
The first sheet is Sheets(1), the next is Sheets(2) and so on.
This remains true even if the names change.

Your macro then looks like this:

Sheets(2).Select
Range("A1:D10").Select
Selection.Delete Shift:=xlToLeft

instead of
Sheets("Randomname").Select
...


For clarity, you may wish to set up some constants to refer to the sheets.
Example: The first sheet always holds ages; it may have begun life called 'Ages' but now is called 'Some Ages'

Declaring constants in the VBA modules such as:

Public Const SHEET_AGES =1
Public Const SHEET_NAMES =2

etc

will allow you to rework the macros with meaningful values, like this:


Sheets(SHEET_NAMES).Select
Range("A1:D10").Select
Selection.Delete Shift:=xlToLeft



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top