VBA - Make Excel save dynamic range as .pdf

Daugaard92

New Member
Joined
Nov 20, 2020
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

My VBA knowledge is unfortunately very limited and therefore, I have to seek advice. I hope I'm asking in the proper fashion and in accordance with forum regulation - Otherwise I sincerely apologise.

I haven't developed the following code - I'm just trying to figure out why it isn't working - I've discovered that most of the code, is directly copy/pasted from "Contextures.com". I have refrained from including the link, as I don't know whether or not I'm allowed to in a post.

From examining the code, I believe that the error lies with this top section. From the "Columns"-section" to the variable declearation. The purpose of this section (I think) is to select a dynamic print-range within the B-column in the sheet, as the print range change. I'm however not skilled enough with VBA to determine why it isn't working. Should a kind soul be able to determine what is wrong, I'd be grateful - And please keep an explanation simple, as I'm very new to VBA :)


VBA Code:
Sub pdf()

Columns("B:B").Select
 
Selection.Find(What:="stop", After:=ActiveCell, LookIn:= _
       xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
       xlNext, MatchCase:=False, SearchFormat:=False).Activate
  
 
  Rrow = ActiveCell.Row

  Range("B1" & ":" & "J" & Rrow + 2).Select

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "ddmmyyyy\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'create default name for savng file
strFile = strName & "Tilbuds_tool_" & strTime & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and File Name to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,848
Messages
6,121,914
Members
449,054
Latest member
luca142

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