Computing.Net > Forums > Programming > Select all Excel sheets with VB

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Select all Excel sheets with VB

Reply to Message Icon

Name: RB
Date: March 16, 2003 at 18:37:41 Pacific
OS: Win2000 Pro
CPU/Ram: P4 1.5 /512 mb
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: A Certain TH
Date: March 17, 2003 at 04:27:18 Pacific
Reply:

Sheets.Select

HTH
Tom


0

Response Number 2
Name: RB
Date: March 17, 2003 at 19:01:38 Pacific
Reply:

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.


0

Response Number 3
Name: A Certain TH
Date: March 18, 2003 at 02:02:52 Pacific
Reply:

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().Select

and 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


0

Response Number 4
Name: RB
Date: March 19, 2003 at 09:17:46 Pacific
Reply:

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 VB

Code-

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 Sub

Thanks
RB


0

Response Number 5
Name: RB
Date: March 19, 2003 at 15:04:07 Pacific
Reply:

Oh yeah Tom...
I appreciate your help.

RB


0

Related Posts

See More



Response Number 6
Name: A Certain TH
Date: March 20, 2003 at 02:31:09 Pacific
Reply:

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 sheetIndexVar

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


0

Response Number 7
Name: RB
Date: March 21, 2003 at 16:48:08 Pacific
Reply:

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


0

Response Number 8
Name: A Certain TH
Date: March 24, 2003 at 06:07:37 Pacific
Reply:

Glad I could help

Tom


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Select all Excel sheets with VB

VB Script and Excel - Sheet Names www.computing.net/answers/programming/vb-script-and-excel-sheet-names/3845.html

combining excel pages with java www.computing.net/answers/programming/combining-excel-pages-with-java/11149.html

Load and resize IMG into cell www.computing.net/answers/programming/load-and-resize-img-into-cell/16497.html