Can you check my code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,689
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I have an excel sheet of which i would like to copy a specific range, then save that range as a pdf file on the desktop.
When the file is saved it just needs to be called LR CODES

The range to copy is A1:K16

I have the code below but not entirely sure its correct,because i copied it from another page & removed some items etc.

Code:
Private Sub CommandButton1_Click()    Dim strFileName As String
    
    strFileName = "C:\Users\Ian\Desktop\ ".pdf"
    If Dir(strFileName) <> vbNullString Then
        MsgBox "SHEET " & " WAS NOT SAVED AS IT ALLREADY EXISTS", vbCritical + vbOKOnly
        Exit Sub
    End If
    
    With ActiveSheet
        .PageSetup.PrintArea = "$A$1:$K$16"
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
        MsgBox "SHEET " & " WAS SAVED SUCCESSFULLY", vbInformation + vbOKOnly
    End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
.
Code:
Option Explicit


Sub SveRngPDF()
Sheets("Sheet1").Range("A1:K16").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Ian\Desktop\Sheet1.pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End Sub
 
Upvote 0
Hi,
I have changed the sheet name & also pdf output name but when i press the button nothing is saved on the desktop.


Code:
Option ExplicitSub SveRngPDF()
Sheets("DR SITE").Range("A1:K16").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Ian\Desktop\LRCODE.pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
End Sub
 
Upvote 0
Hi,
This has worked for me.
Can you advise that when it saves should the pdf already exist show msgbox "File already exists"


Code:
Private Sub CommandButton1_Click()Range("A1:K16").ExportAsFixedFormat xlTypePDF, "C:\Users\Ian\Desktop\Test.pdf"
End Sub
 
Upvote 0
Code:
Option Explicit


Function FileExists(FilePath As String) As Boolean
Dim TestStr As String
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function


Sub SveRngPDF()
Dim Filename As String
Filename = "[COLOR=#333333]C:\Users\Ian\Desktop\Test.pdf[/COLOR]"


If FileExists(Filename) Then
    MsgBox "Exists"
    Exit Sub
Else
    MsgBox "Doesn't Exists"
End If
   
Sheets("Sheet1").Range("A1:K16").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "[COLOR=#333333]C:\Users\Ian\Desktop\Test.pdf[/COLOR]", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True


End Sub
 
Upvote 0
Hi,
I must be applying this wrong as nothing happens at all when i press the command button.

I copied the code above but changed Sheet1 to DR SITE Then put in inside the command button code,so my code is like below.

I keep getting Expected End Sub

Code:
Option ExplicitPrivate Sub CommandButton1_Click()


Function FileExists(FilePath As String) As Boolean
Dim TestStr As String
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function




Sub SveRngPDF()
Dim Filename As String
Filename = "C:\Users\Ian\Desktop\Test.pdf"




If FileExists(Filename) Then
    MsgBox "Exists"
    Exit Sub
Else
    MsgBox "Doesn't Exists"
End If
   
Sheets("DR SITE").Range("A1:K16").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Ian\Desktop\Test.pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True


End Sub
 
Upvote 0
.
This is the only thing I see from your last post that appears incorrect :

Code:
[COLOR=#333333]Option ExplicitPrivate Sub CommandButton1_Click()[/COLOR]

Should be :

Code:
[COLOR=#333333]Option Explicit

Private Sub CommandButton1_Click()[/COLOR]
 
Upvote 0
Hmmm,
Still the same.
Here is a screen shot.


4422.jpg
 
Upvote 0
.
Option Explicit always needs to be at top of page. First thing.

Try moving it and see what happens.
 
Upvote 0
OK done that but i still see the message.
This is shown in yellow
Code:
Private Sub CommandButton1_Click()

Reason being isnt the code supposed to be inside like this.

Code:
Private Sub CommandButton1_Click()
PUT THE CODE HERE
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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