Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello All,
First of all I apologize for the simplicity of this question...I'm very new to VB.
The question "Is there a call to select all worksheets in a Active Excel Workbook?"
I found a procedure to select multiple sheets...
Worksheets(Array("Sheet 1","Sheet 2","Sheet 3")).Select
I want to select all sheets to write to the same range of cells…
The worksheets will increase as the workbook is used.
I can't help but think there is procedure in the libraries to do this.Any help is appreciated.
Thanks
RB

Tom,
I tried that...seemed logical.
I get a Runtime Error 1004...
"Method 'Select' of objects 'Sheets' failed.I've also tried worksheets.select
Thanks for the response.

Hmm. I can promise you that the above works on Excel 97 and 2000 - but may be something to do with the context you are using it in.
Try:
ActiveWorkbook.Sheets.Select
or
ThisWorkbook.Sheets.Select
or
Sheets().Selectand if that fails, then take a look at "where you are" when the code tries to execute. If Excel cannot "see" the sheets objects from where it is, then it will fail. (Think of your context: If my macro said "Get onboard the boat - Pull up the anchor" that would work, but "Get onboard the boat - Lock yourself in the toilet - Pull up the anchor" would only work in very badly designed boats...)
I presume you've tried executing just the one statement on its own?
I'm sorry I can't be much help - error 1004 is not a specific "you've done this, so do this" error. Perhaps you could post up your code?
Tom

Tom,
Believe or not I did try all those suggestion...at least twice.
Again they seemed logical.There is the code...
The function is to copy this "Master Copy" Sheet.
The sheet is a Cnc Mill Tool sheet to discribe (drills, endmills,...ect).
I'm a MFG Engineer doing CNC programming.
This file will store tool data from MasterCam...eventually.
I just thought I'd give you my full intent.
Again...this is my 1st attemp using VBCode-
Sub NewToolSheet()
'
' Macro2 Macro
' Macro recorded 3/15/2003 by RB
''
Dim MySheet As Worksheet 'Variable to represent sheet copied
Dim MySheetNumber As Variant 'Variable for Sheet Number/Name.
Dim SheetNumber As Long 'Variable to use to extract from Sheet.Index
'UnHides Master Copy of ToolSheets to Selecet & copy to end of Book.
Sheets("Master Copy").Visible = True
Sheets("Master Copy").Select
' (Selects Last sheeet)
Sheets("Master Copy").Copy After:=Sheets(Sheets.Count)
Set MySheet = ActiveSheet
Set MySheetNumber = ThisWorkbook.Sheets(Sheets.Count)SheetNumber = MySheetNumber.Index - 1
[MySheet].Name = "Sheet" + Str(SheetNumber)
Sheets("Master Copy").Select
ActiveWindow.SelectedSheets.Visible = False
[MySheet].Select
Set MySheetCount = ActiveWorkbook
' Stores Sheet number on Tool Sheet
Range("P5:P6").Select
ActiveCell.FormulaR1C1 = Str(SheetNumber)
' Sheets(Array(MySheetNumber)).Select ****Not selecting all sheets
ThisWorkBook.Sheets.select
' Range("P9:P10").Select
' ActiveCell.FormulaR1C1 = Str(SheetNumber)
[MySheet].Select
Range("A1").Select
End SubThanks
RB

Hi RB
This is where you need to remember the "where you are" type rules.
Looking at what you've done:
1) Created a new worksheet
2) Hidden a worksheet
3) Asked Excel to select ALL worksheets.Clearly, its not going to manage 3 in the wake of 2. If you get rid of the hidden property then it will work.
But - you don't want to affect your master sheet... so:
The simple solution is, rather than try to hide it to avoid changes, you could move it out of the workbook and then move it back in the end. (Or copy it to another workbook and past it back - it would depend again on your context)
Another solution is to do something like:
Dim sheetIndexVar as Integer
For sheetIndexVar = 2 to Sheets.Count
With Sheets(sheetIndexVar)
...
End With
Next sheetIndexVarFINALLY: I would say the following: Think about why you are hiding your master sheet. Then think about whether your master should be part of your working book, or should be referenced from it. This could solve all your problems....
I hope this helps you!
Tom

Tom,
That was it!!!
I understand what you telling me about "where you are".
The reason I was hidding the sheet is I didn't want it to be messed with.
I know you can simply unhide...but the people using this would not look for it.
I think I'll bring it in through another workbook.Tom...thanks for all your help.
I do appreciate it.Thanks again
RB

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |