First post so please excuse any poor etiquette.
I am writing a VBA which produces an information pack for our customers to use. This pack is built to be dynamic based on certain user selections - for example FAQ pages will be specific to selections made by the user. I have built each module to add the relevant sheet names to a contents sheet as it runs. At the end, I pick up this list of sheets, select them and produce a PDF.
What I am trying to do is hide all of the other sheets other than the ones that are selected - I can do this by calling them specifically by name, or by adding "zzz" to the end of sheet names which are pure reference sheets, but I am hoping there is a better way.
The code I want would do this:
Code for producing the PDF is below - note that due to some ill-planned naming, "Contents Array" is a named range on the contents sheet, and "ContentsList" is the VBA array:
For Each cell In wsContents.Range("ContentsArray")
ContentsList(j) = cell.Value
wb1.Sheets(cell.Value).Select
j = j + 1
Next 'cell
wb1.Sheets(ContentsList()).Select
FileName = wsControl.Range("CustomerName") & " Pack " & Format(wsControl.Range("ReportDate"), "dd-mm-yyyy")
PDFFilePath = wb1.Path & "/AutoGenerated Packs/" & FileName & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=PDFFilePath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
First, you can set your array to the range in one go:
ContentsList = wsContents.Range("ContentsArray").Value
You will then need to loop through your array for each sheet in your workbook to check if it exists... Something like the below (if you use the same method for the array as you already have, else you will need another way to determine j
(the number of elements in your array)):
Dim ws As Worksheet
Dim i As Long
For Each ws In Thisworkbook.Worksheets
For i = 1 To j
If ws.Name = ContentsList(i) Then
'if found exit for as we do not want to hide
Exit For
End If
'if not found then hide
If i = j Then
ws.Visible = xlSheetHidden
End If
Next j
Next ws
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.