Select sheet based on value in cell

Hatye

Board Regular
Joined
Jan 11, 2010
Messages
143
Hi,

I got an workbook I want to save as pdf. I want the pdf to contain multiple sheets, and that can be done by selecting each sheet and then save it as pdf. The problem is that I don't know which sheet to included. That is because I want to see if it is contents in cell A7, and if so: include that sheet in the selecting-properties.

You can use this code to select sheets:
Code:
Sheets(Array("Rapport", Rapport_Punkt)).Select

I want to see if any of the sheets "Rapport_Linjer", "Rapport_Punkt", "Rapport_Polygon", "Rapport_Tekst" and "Rapport_Triangelnett" got contents in the cell A7, and if so: include the sheet in the save as pdf function.

Please let me know if you want to see more of the code, to get the context.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi Hatye,

This will only select those sheets in the array that have something in cell A7:

Code:
Option Explicit
Sub Macro1()

    Dim varMyArray() As Variant 'Declares a dynamic array variable
    Dim lngArrayCount As Long
    Dim varMySheet As Variant
        
    For Each varMySheet In Array("Rapport_Linjer", "Rapport_Punkt", "Rapport_Polygon", "Rapport_Tekst", "Rapport_Triangelnett")
        If Len(Sheets(varMySheet).Range("A7")) > 0 Then
            lngArrayCount = lngArrayCount + 1
            ReDim Preserve varMyArray(1 To lngArrayCount) 'Append the record to the existing array
            varMyArray(lngArrayCount) = varMySheet
        End If
    Next varMySheet
    
    Sheets(varMyArray).Select
    
    Erase varMyArray
    
End Sub

HTH

Robert
 
Upvote 0
Thank you so much for your reply Trebor76,

It seems like it is working, but I forgot to write that I also need to insert the sheet "Rapport" at all time.

Tried the following
Code:
Sheets(array("Rapport"), varMyArray).Select
but that didn't work. I got an compile error.

Do you know how to fix that? :)
 
Upvote 0
You need to add the sheet name in the first array like so:

Code:
For Each varMySheet In Array("Rapport", "Rapport_Linjer", "Rapport_Punkt", "Rapport_Polygon", "Rapport_Tekst", "Rapport_Triangelnett")
 
Upvote 0
But the sheet "Rapport" doesn't contain any data in the cell A7. It is a sort of first page on an report that always should be included.
 
Upvote 0
Then as well as adding Rapport to the original array as above, put this line of code directly beneath it:

Code:
If Len(Sheets(varMySheet).Range("A7")) > 0 Or varMySheet = "Rapport" Then
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,937
Members
448,534
Latest member
benefuexx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top