Excel 2016 - VBA - Select sheets if data

CrashOD

Board Regular
Joined
Feb 5, 2019
Messages
118
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
I'm looking for it to sleect sheets if on sheet1 (Recepit Ledger)
B49, C49, D49, I49, J49, K49 is above 0
B49 & or L49 is above 0 it selects worksheet2(WS Dis)
C49 & or J49 is above 0 it selects worksheet3(ws Face)
D49 & or K49 above 0 it selects worksheet4(ws Penalty)
it also is not printing I took this code from diffrent sheet i programed. so the directory exest same one it prints to in other sheet. and does not show a error just does what it does like it worked.

im trying to add the code to this vba code
Sub Print_PDF()

' Print to PDF
Sheets(Array("Recepit Ledger", "New County", "New City")).Select
Sheets("Recepit Ledger").Activate

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="T:\2022_TAX_CERTS\" & Sheets(1).Range("I1").Value & " - " & " County & Local Report " & Sheets(1).Range("B1").Value & " - Bills " & Sheets(1).Range("L1").NumberFormat = "mm-dd-yyyy" & " - " & Sheets(1).Range("N1").NumberFormat = "mm-dd-yyyy", _
OpenAfterPublish:=False, ignoreprintareas:=False

End Sub

here is the sheet for it to refrence

Shamokin City - Report_v1_2 (1).xlsm
ABCDEFGHIJKLMN
1Report7Shamokin City Reconciliation Work Sheet202305-02-2022Through05-31-2022
2Shamokin City Reconciliation Work SheetCounty & City PersonalBills #County & City Real Estate
3DATEDiscountFacePenaltyDiscountFacePenaltyDATEDiscountFacePenaltyDiscountFacePenalty
405/03/2022020$0.00$97.33$0.0005/02/2022040$0.00$1,241.37$0.00
505/05/2022030$0.00$30.00$0.0005/03/2022050$0.00$2,491.65$0.00
605/06/2022080$0.00$157.33$0.0005/05/2022030$0.00$1,566.15$0.00
705/09/2022070$0.00$430.87$0.0005/06/2022030$0.00$1,294.75$0.00
805/10/2022010$0.00$10.00$0.0005/09/2022070$0.00$3,666.23$0.00
905/12/2022030$0.00$261.98$0.0005/10/2022050$0.00$1,935.45$0.00
1005/13/2022050$0.00$410.86$0.0005/12/2022010$0.00$395.99$0.00
1105/16/2022070$0.00$147.33$0.0005/13/2022080$0.00$3,704.50$0.00
1205/18/2022070$0.00$456.64$0.0005/16/2022080$0.00$5,793.00$0.00
1305/19/2022030$0.00$287.75$0.0005/18/20220110$0.00$5,875.76$0.00
1405/20/2022030$0.00$81.55$0.0005/19/2022010$0.00$547.26$0.00
1505/20/2022020$0.00$97.33$0.0005/20/2022010$0.00$405.78$0.00
1605/23/2022010$0.00$87.33$0.0005/20/2022020$0.00$885.41$0.00
1705/23/2022040$0.00$91.55$0.0005/23/2022070$0.00$3,105.58$0.00
1805/27/2022060$0.00$266.21$0.0005/23/2022030$0.00$1,381.94$0.00
1905/31/2022060$0.00$369.31$0.0005/24/2022010$0.00$711.88$0.00
2005/25/2022010$0.00$1,036.68$0.00
2105/27/2022050$0.00$3,234.63$0.00
2205/31/2022040$0.00$1,103.43$0.00
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47Total Bills:68Total $:$3,283.37Total Bills:80Total $:$40,377.44
48
490680$0.00$3,283.37$0.000800$0.00$40,377.44$0.00
Recepit Ledger
Cell Formulas
RangeFormula
L1L1=MIN(A4:A46, H4:H46)
N1N1=MAX(A4:A46, H4:H46)
D47,K47D47=SUM(B4:D46)
G47,N47G47=SUM(E49+F49+G49)
I49:N49,B49:G49B49=SUM(B4:B46)
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
VBA Code:
Function SheetExists(shtName As Variant, wbk As Workbook) As Boolean
If IsNumeric(shtName) Then
SheetExists = CBool(Not wbk.Sheets(CInt(shtName)) Is Nothing)
Else
SheetExists = CBool(Not wbk.Sheets(shtName) Is Nothing)
End If
End Function

Sub Print_PDF_v2()
On Error Resume Next
Dim wbk As Workbook
Set wbk = ThisWorkbook ' or set it to the appropriate workbook
Dim ws As Worksheet
Dim selectedSheets As Collection
Set selectedSheets = New Collection
Dim path As String
Dim newWbk As Workbook

' Always add Receipt Ledger, New County, and New City sheets
selectedSheets.Add wbk.Sheets("Receipt Ledger")
selectedSheets.Add wbk.Sheets("New County")
selectedSheets.Add wbk.Sheets("New City")

' Check if WS DIS sheet should be added based on B52 and/or I52 value
If Not SheetExists("WS DIS", wbk) Then
    MsgBox "WS DIS sheet does not exist"
    Exit Sub
ElseIf wbk.Sheets("Receipt Ledger").Range("B52").Value >= 1 Or _
wbk.Sheets("Receipt Ledger").Range("I52").Value >= 1 Then
    selectedSheets.Add wbk.Sheets("WS DIS")
End If

' Check if WS Face sheet should be added based on C52 and/or J52 value
If Not SheetExists("WS FACE", wbk) Then
    MsgBox "WS FACE sheet does not exist"
    Exit Sub
ElseIf wbk.Sheets("Receipt Ledger").Range("C52").Value >= 1 Or _
wbk.Sheets("Receipt Ledger").Range("J52").Value >= 1 Then
    selectedSheets.Add wbk.Sheets("WS FACE")
End If

' Check if WS PEN sheet should be added based on D52 and/or K52 value
If Not SheetExists("WS PEN", wbk) Then
    MsgBox "WS PEN sheet does not exist"
    Exit Sub
ElseIf wbk.Sheets("Receipt Ledger").Range("D52").Value >= 1 Or _
wbk.Sheets("Receipt Ledger").Range("K52").Value >= 1 Then
    selectedSheets.Add wbk.Sheets("WS PEN")
End If

If selectedSheets.Count >= 1 Then
  '  path = Application.GetSaveAsFilename(wbk.Sheets("Receipt Ledger").Range("O1").Value & wbk.Sheets("Receipt Ledger").Range("I1").Value & " - C&L - Report " & wbk.Sheets("Receipt Ledger").Range("B1").Value & ".pdf") ', "PDF Files (*.pdf), *.pdf")
    path = (wbk.Sheets("Receipt Ledger").Range("O2").Value & "\" & wbk.Sheets("Receipt Ledger").Range("I1").Value & " - C&L - Report " & wbk.Sheets("Receipt Ledger").Range("B1").Value & ".pdf") ', "PDF Files (*.pdf), *.pdf")
 
    If path <> False Then
        Set newWbk = Workbooks.Add
        Dim orderArray() As Variant
        orderArray = Array("Receipt Ledger", "WS DIS", "WS FACE", "WS PEN", "New County", "New City")
        For Each sheetName In orderArray
            For Each ws In selectedSheets
                If ws.Name = sheetName Then
                    ws.Copy After:=newWbk.Sheets(newWbk.Sheets.Count)
                End If
            Next ws
        Next sheetName
        newWbk.ExportAsFixedFormat Type:=xlTypePDF, fileName:=path, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        newWbk.Close False
        MsgBox "Selected sheets have been saved to PDF successfully."
        ' Add code to save to 2nd location
      ' Dim secondPath As String
'        secondPath = "D:\Country&LocalReport\" & (wbk.Sheets("Receipt Ledger").Range("O1").Value & " - " & wbk.Sheets("Receipt Ledger").Range("I1").Value & " - C&L - Report " & wbk.Sheets("Receipt Ledger").Range("B1").Value & ".pdf")
    'secondPath = "D:\Country&LocalReport\" & wbk.Sheets("Receipt Ledger").Range("O1").Value & " - " & wbk.Sheets("Receipt Ledger").Range("I1").Value & " - C&L - Report " & wbk.Sheets("Receipt Ledger").Range("B1").Value & ".pdf"
     '   If Dir(secondPath, vbDirectory) <> "" Then
      '      newWbk.ExportAsFixedFormat Type:=xlTypePDF, fileName:=secondPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
       ' Else
            ' Do nothing
        'End If
    End If
End If
End Sub
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,223,661
Messages
6,173,647
Members
452,525
Latest member
DPOLKADOT

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