Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Trefor
 
Posts: n/a
Default Error 1004 using array in Sheet select.

If I select a sheet with its real name:

Sheets("Sheet1").Select - Works fine
Sheets(Array("Sheet1", "Sheet2")).Select - Works fine

If I assign the sheet name(s) to a constant

Public Const TSGSheet1 = "Sheet1"
Public Const TSGSheet2 = "Sheet2"

Sheets(TSGSheet1).Select - Works fine
Sheets(TSGSheet2).Select - Works fine

Sheets(Array(TSGSheet1, TSGSheet2)).Select - fails with Error 1004

My macro's/VBA do multiple Selects and rather than explicitly specifying the
sheet names in every command, I thought I would set up a constant, use the
constant in the multiple commands. If I then wanted to change the sheet name,
I could just change the constant assignment once.

Have I done something wrong? It would appear that you can not use constants
in an array?

--
Trefor
  #2   Report Post  
Jim Cone
 
Posts: n/a
Default

Trefor,

It works for me and selects the two sheets,
if the public constants are placed at the top of the module above all the subs.

Jim Cone
San Francisco, USA


"Trefor"
wrote in message

If I select a sheet with its real name:

Sheets("Sheet1").Select - Works fine
Sheets(Array("Sheet1", "Sheet2")).Select - Works fine

If I assign the sheet name(s) to a constant
Public Const TSGSheet1 = "Sheet1"
Public Const TSGSheet2 = "Sheet2"
Sheets(TSGSheet1).Select - Works fine
Sheets(TSGSheet2).Select - Works fine
Sheets(Array(TSGSheet1, TSGSheet2)).Select - fails with Error 1004

My macro's/VBA do multiple Selects and rather than explicitly specifying the
sheet names in every command, I thought I would set up a constant, use the
constant in the multiple commands. If I then wanted to change the sheet name,
I could just change the constant assignment once.
Have I done something wrong? It would appear that you can not use constants
in an array?
--
Trefor
  #3   Report Post  
Dave Peterson
 
Posts: n/a
Default

And it worked for me, too.

Are you sure that the active workbook had sheets with those two names?


Trefor wrote:

If I select a sheet with its real name:

Sheets("Sheet1").Select - Works fine
Sheets(Array("Sheet1", "Sheet2")).Select - Works fine

If I assign the sheet name(s) to a constant

Public Const TSGSheet1 = "Sheet1"
Public Const TSGSheet2 = "Sheet2"

Sheets(TSGSheet1).Select - Works fine
Sheets(TSGSheet2).Select - Works fine

Sheets(Array(TSGSheet1, TSGSheet2)).Select - fails with Error 1004

My macro's/VBA do multiple Selects and rather than explicitly specifying the
sheet names in every command, I thought I would set up a constant, use the
constant in the multiple commands. If I then wanted to change the sheet name,
I could just change the constant assignment once.

Have I done something wrong? It would appear that you can not use constants
in an array?

--
Trefor


--

Dave Peterson
  #4   Report Post  
Trefor
 
Posts: n/a
Default

Sorry misled you a bit.

Try the following (same constants as before)

Sheets(TSGSheet1).Visible = True
Sheets(TSGSheet2).Visible = True

Sheets(Array(TSGSheet1, TSGSheet2)).Visible = False

Sheets(Array(TSGSheet1, TSGSheet2)).Visible = True <<<<< This fails

The line in my original question failed because one sheet was hidden. The
last line above fails with the same error.
--
Trefor


"Dave Peterson" wrote:

And it worked for me, too.

Are you sure that the active workbook had sheets with those two names?


Trefor wrote:

If I select a sheet with its real name:

Sheets("Sheet1").Select - Works fine
Sheets(Array("Sheet1", "Sheet2")).Select - Works fine

If I assign the sheet name(s) to a constant

Public Const TSGSheet1 = "Sheet1"
Public Const TSGSheet2 = "Sheet2"

Sheets(TSGSheet1).Select - Works fine
Sheets(TSGSheet2).Select - Works fine

Sheets(Array(TSGSheet1, TSGSheet2)).Select - fails with Error 1004

My macro's/VBA do multiple Selects and rather than explicitly specifying the
sheet names in every command, I thought I would set up a constant, use the
constant in the multiple commands. If I then wanted to change the sheet name,
I could just change the constant assignment once.

Have I done something wrong? It would appear that you can not use constants
in an array?

--
Trefor


--

Dave Peterson

  #5   Report Post  
Dave Peterson
 
Posts: n/a
Default

Yep.

I think you'll want to do one sheet at a time.

Trefor wrote:

Sorry misled you a bit.

Try the following (same constants as before)

Sheets(TSGSheet1).Visible = True
Sheets(TSGSheet2).Visible = True

Sheets(Array(TSGSheet1, TSGSheet2)).Visible = False

Sheets(Array(TSGSheet1, TSGSheet2)).Visible = True <<<<< This fails

The line in my original question failed because one sheet was hidden. The
last line above fails with the same error.
--
Trefor

"Dave Peterson" wrote:

And it worked for me, too.

Are you sure that the active workbook had sheets with those two names?


Trefor wrote:

If I select a sheet with its real name:

Sheets("Sheet1").Select - Works fine
Sheets(Array("Sheet1", "Sheet2")).Select - Works fine

If I assign the sheet name(s) to a constant

Public Const TSGSheet1 = "Sheet1"
Public Const TSGSheet2 = "Sheet2"

Sheets(TSGSheet1).Select - Works fine
Sheets(TSGSheet2).Select - Works fine

Sheets(Array(TSGSheet1, TSGSheet2)).Select - fails with Error 1004

My macro's/VBA do multiple Selects and rather than explicitly specifying the
sheet names in every command, I thought I would set up a constant, use the
constant in the multiple commands. If I then wanted to change the sheet name,
I could just change the constant assignment once.

Have I done something wrong? It would appear that you can not use constants
in an array?

--
Trefor


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
is it possible to select and change the values of cells in a minimized excel spread sheet from vba? Daniel Excel Worksheet Functions 6 July 11th 05 11:24 PM
Formula checking multiple worksheets sonic-the-mouse Excel Worksheet Functions 2 June 5th 05 07:48 PM
Formula checking multiple worksheets sonic-the-mouse Excel Worksheet Functions 2 June 5th 05 03:28 AM
Dynamic Formulas with Dynamic Ranges Ralph Howarth Excel Worksheet Functions 5 January 21st 05 08:44 AM
how can i select all the cells with same color on a sheet if there are multipale colors by vba code uobt Charts and Charting in Excel 1 December 15th 04 05:27 PM


All times are GMT +1. The time now is 11:14 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"