#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default ARRAYS

Hi there,

Having a problem with the following code

Sub mcrPrint()
Dim prange As String

Sheets("macroinputs").Range("N1").Value = InputBox("Enter
Section No to print/preview or ALL", , "ALL")
Sheets("macroinputs").Range("N2").Value = InputBox("Print
(Y) or Preview (N)", , "N")
prange = Sheets("macroinputs").Range("O8").Value

If Sheets("macroinputs").Range("N1").Value < "All"
Then
Sheets(Array(prange)).Select
If Sheets("macroinputs").Range("N2").Value = "Y"
Then
ActiveWindow.SelectedSheets.PrintOut
Copies:=1, Collate:=True
Else
ActiveWindow.SelectedSheets.PrintPreview
End If
End If
End Sub

I get an error "Subscript out of range" run-time error 9

What I am trying to do is as follows:-
The excle file has over 100 sheets, some of which are
hidden. Sheets are broken down into 10 sections, but
there may only be 4 sheets in a given section that
require printing.

I use a table to list which sheets are hidden, or not.

To get the value for prange - as in the code, I use a
lookup on this table.

The data in prange is currently
"CAT0", "00", "01", "06", "07", "08", "09"

as you can see, sheets "02", "03", "04", "05" are hidden,
and do not require printing.

When I replace prange in the vb code, with this value,
the macro works just fine.

What can I do ?

  #2   Report Post  
Posted to microsoft.public.excel.programming
acw acw is offline
external usenet poster
 
Posts: 19
Default ARRAYS

Gary

Change the line
prange = Sheets("macroinputs").Range("O8").Value to
prange = evaluate("={" & Range("O8").Value & "}")

and the line
Sheets(Array(prange)).Select to
sheets(prange).select

I think there is a more elegant way to do this but not
sure what it is.

Tony

-----Original Message-----
Hi there,

Having a problem with the following code

Sub mcrPrint()
Dim prange As String

Sheets("macroinputs").Range("N1").Value = InputBox("Enter
Section No to print/preview or ALL", , "ALL")
Sheets("macroinputs").Range("N2").Value = InputBox("Print
(Y) or Preview (N)", , "N")
prange = Sheets("macroinputs").Range("O8").Value

If Sheets("macroinputs").Range("N1").Value < "All"
Then
Sheets(Array(prange)).Select
If Sheets("macroinputs").Range("N2").Value = "Y"
Then
ActiveWindow.SelectedSheets.PrintOut
Copies:=1, Collate:=True
Else
ActiveWindow.SelectedSheets.PrintPreview
End If
End If
End Sub

I get an error "Subscript out of range" run-time error 9

What I am trying to do is as follows:-
The excle file has over 100 sheets, some of which are
hidden. Sheets are broken down into 10 sections, but
there may only be 4 sheets in a given section that
require printing.

I use a table to list which sheets are hidden, or not.

To get the value for prange - as in the code, I use a
lookup on this table.

The data in prange is currently
"CAT0", "00", "01", "06", "07", "08", "09"

as you can see, sheets "02", "03", "04", "05" are hidden,
and do not require printing.

When I replace prange in the vb code, with this value,
the macro works just fine.

What can I do ?

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default ARRAYS

prange = Evaluate( "{" & Sheets("macroinputs").Range("O8").Value & "}")

Should work.

Regards,
Tom Ogilvy


Gary B wrote in message
...
Hi there,

Having a problem with the following code

Sub mcrPrint()
Dim prange As String

Sheets("macroinputs").Range("N1").Value = InputBox("Enter
Section No to print/preview or ALL", , "ALL")
Sheets("macroinputs").Range("N2").Value = InputBox("Print
(Y) or Preview (N)", , "N")
prange = Sheets("macroinputs").Range("O8").Value

If Sheets("macroinputs").Range("N1").Value < "All"
Then
Sheets(Array(prange)).Select
If Sheets("macroinputs").Range("N2").Value = "Y"
Then
ActiveWindow.SelectedSheets.PrintOut
Copies:=1, Collate:=True
Else
ActiveWindow.SelectedSheets.PrintPreview
End If
End If
End Sub

I get an error "Subscript out of range" run-time error 9

What I am trying to do is as follows:-
The excle file has over 100 sheets, some of which are
hidden. Sheets are broken down into 10 sections, but
there may only be 4 sheets in a given section that
require printing.

I use a table to list which sheets are hidden, or not.

To get the value for prange - as in the code, I use a
lookup on this table.

The data in prange is currently
"CAT0", "00", "01", "06", "07", "08", "09"

as you can see, sheets "02", "03", "04", "05" are hidden,
and do not require printing.

When I replace prange in the vb code, with this value,
the macro works just fine.

What can I do ?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default ARRAYS


Thanks for the input, but alas, now get an error

Run-time error 13 - Type mismatch on

prange = evaluate("={" & Range("O8").Value & "}")

I have not used the evaluate function before - sorry !!


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default ARRAYS

? Range("O8")
"CAT0", "00", "01", "06", "07", "08", "09"
prange = Evaluate("{" & Range("O8").Value & "}")
? prange(1)
CAT0
? prange(7)
09
Worksheets(prange).Select
? activewindow.SelectedSheets.Count
7

works fine for me. Demod from the immediate window.

Regards,
Tom Ogilvy


Gary Burke wrote in message
...

Thanks for the input, but alas, now get an error

Run-time error 13 - Type mismatch on

prange = evaluate("={" & Range("O8").Value & "}")

I have not used the evaluate function before - sorry !!


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default ARRAYS


Hi Tom,

Many thanks for your patience, however, same error is still being
returned. (I had tried including the sheet name before responding last
time).

I have also tried with data as follows:-

a) "DEPT TTL", "CAT0", "10", "11", "13", "16"

b) DEPT TTL", "CAT0", "10", "11", "13", "16 - note the missing starting
and ending "

Any other thoughts ?



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default ARRAYS

I showed what was in my Range("O8") and that it worked for me.

I don't know what else to tell you. That is about the only way to do it if
you have this string in a single cell.

sStr = """CAT0"", ""00"", ""01"", ""07"", ""08"""
prange = Array(sStr)

does not work

sStr = """CAT0"", ""00"", ""01"", ""07"", ""08"""
prange = Evaluate( "{" & sStr & "}")

Here, run this code:

Sub Tester4()
Dim sStr As String
Dim prange As Variant
Dim i As Long
sStr = """CAT0"", ""00"", ""01"", ""07"", ""08"""
prange = Evaluate("{" & sStr & "}")
Debug.Print sStr
For i = LBound(prange) To UBound(prange)
Debug.Print i, prange(i), Worksheets(prange(i)).Name
Next
End Sub

this produced:
"CAT0", "00", "01", "07", "08"
1 CAT0 CAT0
2 00 00
3 01 01
4 07 07
5 08 08

If you don't know how to use the immediate window:

Sub Tester4()
Dim sStr As String
Dim prange As Variant
Dim i As Long
sStr = """CAT0"", ""00"", ""01"", ""07"", ""08"""
prange = Evaluate("{" & sStr & "}")
MsgBox sStr
For i = LBound(prange) To UBound(prange)
msgbox i & " " & prange(i) & " " & Worksheets(prange(i)).Name
Next
End Sub

You don't have to use ""00"" in the cell - that is just for building a
string directly.
Regards,
Tom Ogilvy


Gary Burke wrote in message
...

Hi Tom,

Many thanks for your patience, however, same error is still being
returned. (I had tried including the sheet name before responding last
time).

I have also tried with data as follows:-

a) "DEPT TTL", "CAT0", "10", "11", "13", "16"

b) DEPT TTL", "CAT0", "10", "11", "13", "16 - note the missing starting
and ending "

Any other thoughts ?



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default ARRAYS


Hi Tom,

Ran the demod as suggested, and DONT get the type mismatch error,
however, when the macro runs, I do get the error, although the macro
does comile OK.

Could there be something missing from my preferences ?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default ARRAYS


found the problem

dim prange as string

should be

dim prange as variant

Thanks for all the help

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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
Avg Arrays PAL Excel Worksheet Functions 2 February 13th 09 06:02 PM
Use of IF with arrays Andrew L Excel Worksheet Functions 5 August 15th 08 10:15 PM
Use of arrays DKS Excel Worksheet Functions 1 November 30th 06 08:38 PM
Use of arrays Dave F Excel Worksheet Functions 0 November 30th 06 04:26 PM
Use of arrays DKS Excel Worksheet Functions 0 November 30th 06 04:11 PM


All times are GMT +1. The time now is 05:55 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"