mod code should print after save as pdf

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
995
Office Version
  1. 2010
hello experts
I have this code works and I need fix it what I want save the file as pdf and show the message what is existed in the code and if press yes then print the file if no then exit without do any thing I appreciate if any body has suggestion
VBA Code:
Private Sub CommandButton1_Click()
If ListBox1.ListCount = 0 Then
    MsgBox "No data to print"
    Exit Sub
  End If
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  Dim l2 As Workbook, ans As Variant, sPath As String, sname As String
 
  ans = MsgBox("you sure print", vbQuestion + vbYesNo)
  If ans = vbNo Then Exit Sub
 
  sPath = "C:\Users\ose\Desktop\"
 
  sname = Sheets("Sheet1").Range("H2").Value
 
  Sheets("Sheet1").Copy
  Set l2 = ActiveWorkbook
  l2.Sheets(1).Rows("2:" & Rows.Count).ClearContents
  Range("A2").Resize(ListBox1.ListCount, ListBox1.ColumnCount).Value = ListBox1.List
  l2.Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=sPath & sname, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True
  l2.Close False
End Sub
thanks in advance
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Watch MrExcel Video

Forum statistics

Threads
1,129,682
Messages
5,637,769
Members
416,982
Latest member
lisam77

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
Top