PC Review


Reply
Thread Tools Rate Thread

Array To View Specific Sheet(s)

 
 
GEdwards
Guest
Posts: n/a
 
      25th Mar 2010
Using a macro I need to view specific worksheets if they have content. I
will always want my "Invoice" sheet and if on the Invoice sheet, cell B12 has
a value I want to view the Invoice and Labour sheet. If cell B13 has a value
I also want to view the Materials sheet.

My macro, shown below, halts at the line "Sheets(Array(prtWhat)).Select".

How can I dynamically build the proper statement...
"Sheets(Array("Invoice", "Labour", "Materials")).Select"

Please help.

Sub ViewTest()

prtWhat = "Invoice"
Labour = ", Labour"
Materials = ", Materials"
If Range("B12").Value <> "" Then
prtWhat = prtWhat & Labour
Else
prtWhat = prtWhat
End If
If Range("B13").Value <> "" Then
prtWhat = prtWhat & Materials
Else
prtWhat = prtWhat
End If

MsgBox (prtWhat)

Sheets(Array(prtWhat)).Select
Sheets("Invoice").Activate
ActiveWindow.SelectedSheets.PrintPreview
Sheets("Invoice").Select
End Sub
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      25th Mar 2010
Don't use the Array function, rather, use the Split function... its first
argument is a String which you can build dynamically. For example...

Dim SheetStr As String
...........
...........
SheetStr = "Invoice"
With Sheets(SheetStr)
If .Range("B12").Value <> "" Then SheetStr = SheetStr & ",Labour"
If .Range("B13").Value <> "" Then SheetStr = SheetStr & ",Materials"
End With
Sheets(Split(SheetStr, ",")).Select

Note the comma in front of the additional sheet names that are being
concatenated... that is the delimiter that is used in the Split function
call (don't add any spaces around those comma or else the spaces will end up
in the sheet names).

--
Rick (MVP - Excel)



"GEdwards" <(E-Mail Removed)> wrote in message
news:7C99D243-716F-4E18-AF5E-(E-Mail Removed)...
> Using a macro I need to view specific worksheets if they have content. I
> will always want my "Invoice" sheet and if on the Invoice sheet, cell B12
> has
> a value I want to view the Invoice and Labour sheet. If cell B13 has a
> value
> I also want to view the Materials sheet.
>
> My macro, shown below, halts at the line "Sheets(Array(prtWhat)).Select".
>
> How can I dynamically build the proper statement...
> "Sheets(Array("Invoice", "Labour", "Materials")).Select"
>
> Please help.
>
> Sub ViewTest()
>
> prtWhat = "Invoice"
> Labour = ", Labour"
> Materials = ", Materials"
> If Range("B12").Value <> "" Then
> prtWhat = prtWhat & Labour
> Else
> prtWhat = prtWhat
> End If
> If Range("B13").Value <> "" Then
> prtWhat = prtWhat & Materials
> Else
> prtWhat = prtWhat
> End If
>
> MsgBox (prtWhat)
>
> Sheets(Array(prtWhat)).Select
> Sheets("Invoice").Activate
> ActiveWindow.SelectedSheets.PrintPreview
> Sheets("Invoice").Select
> End Sub


 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      25th Mar 2010
Try the below....It is always better to mention the sheet name while
referring to the range. Instead of Range("B12") mention as
Sheets("Sheetname").Range("B12")


Sub ViewTest()
Dim strSheets As String

strSheets = "Invoice"
If Range("B12") <> "" Then strSheets = strSheets & ",Labour"
If Range("B13") <> "" Then strSheets = strSheets & ",Materials"

Sheets(Split(strSheets, ",")).Select
Sheets("Invoice").Activate
ActiveWindow.SelectedSheets.PrintPreview
End Sub




--
Jacob


"GEdwards" wrote:

> Using a macro I need to view specific worksheets if they have content. I
> will always want my "Invoice" sheet and if on the Invoice sheet, cell B12 has
> a value I want to view the Invoice and Labour sheet. If cell B13 has a value
> I also want to view the Materials sheet.
>
> My macro, shown below, halts at the line "Sheets(Array(prtWhat)).Select".
>
> How can I dynamically build the proper statement...
> "Sheets(Array("Invoice", "Labour", "Materials")).Select"
>
> Please help.
>
> Sub ViewTest()
>
> prtWhat = "Invoice"
> Labour = ", Labour"
> Materials = ", Materials"
> If Range("B12").Value <> "" Then
> prtWhat = prtWhat & Labour
> Else
> prtWhat = prtWhat
> End If
> If Range("B13").Value <> "" Then
> prtWhat = prtWhat & Materials
> Else
> prtWhat = prtWhat
> End If
>
> MsgBox (prtWhat)
>
> Sheets(Array(prtWhat)).Select
> Sheets("Invoice").Activate
> ActiveWindow.SelectedSheets.PrintPreview
> Sheets("Invoice").Select
> End Sub

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a new sheet from specific data in existing sheet Rosscoe Microsoft Excel Misc 3 13th Jan 2010 06:08 PM
Find specific value in array of array formula DzednConfsd Microsoft Excel Worksheet Functions 2 13th Jan 2009 06:19 AM
How to Automatically transfer specific Data from Sheet 1 to Sheet =?Utf-8?B?Sm1hbg==?= Microsoft Excel Worksheet Functions 12 10th May 2007 05:35 AM
Send data from userform to specific cell on specific sheet? =?Utf-8?B?SmVubkxlZQ==?= Microsoft Excel Programming 10 10th Mar 2007 02:55 AM
downcasting from a System.Array to an array of a specific type =?Utf-8?B?Sm9lIERveWxl?= Microsoft C# .NET 4 3rd Nov 2005 09:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 PM.