mod code should print after save as pdf

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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