Compile Error: Sub or Function not defined... Why? - excel

So this is my code that´s is supposed to loop through the pivot's SUPPLIER FILTER based on a list located in the same worksheet. After every loop it has to upload the file to the intranet in a .pdf format.
Sub Upload()
Dim pt As PivotTable
Dim pi As PivotItem
Dim pf As PivotField
Dim lLoop As Long
Set pt = Sheets("To Supplier").PivotTables("PivotTable1")
Set pf = pt.PivotFields("[Query].[SUPPLIER].[SUPPLIER]")
Sheets("To Supplier").Select
For Each pi In pf.PivotItems
On Error Resume Next
pf.CurrentPage = pi.Value
On Error GoTo 0
If pf.CurrentPage = pi.Value Then
If lLoop = 0 Then
With Sheets("To Supplier").PageSetup
.CenterFooter = pi.Value
.LeftHeader = pt.Name
.LeftFooter = Now
End With
End If
Range("L2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(RC[-6]:R[59]C[-5],MATCH(R[1]C[-10],RC[-6]:R[59]C[-6],0),2)"
Sheets("To Supplier").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"http://collaborationx.com/portalone/sourcing/Supplier%20documents/" & Cell("L2").Value _
& "/Evaluations/" & Cell("L2").Value & "%20Credits.pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
lLoop = lLoop + 1
End If
Next pi
End Sub
However, It keeps throwing me off... now the prob is that it says the function is not defined, before the problem was with the references...

Cell("L2") is not defined. You probably wanted Range("L2") but since you selected that range before you could use ActiveCell. Example, HTH.
Option Explicit
Sub test()
Range("L2").Select
ActiveCell.FormulaR1C1 = "=INDEX(RC[-6]:R[59]C[-5],MATCH(R[1]C[-10],RC[-6]:R[59]C[-6],0),2)"
Dim exportFileName As String
exportFileName = "http://collaborationx.com/portalone/sourcing/Supplier%20documents/{0}/Evaluations/{0}%20Credits.pdf"
exportFileName = Replace(exportFileName, "{0}", ActiveCell.Value)
Sheets("To Supplier").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=exportFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub

Related

In this Excel VBA code, how would I specify to only include A1:M500 from Sheet1 all else being the same?

Sub SavetoPDF()
Application.ScreenUpdating = False
Dim UseName As Variant
Dim printRanges As Range
UseName = Application.GetSaveAsFilename( _
InitialFileName:="Report.pdf", _
FileFilter:="PDF files, *.pdf", _
Title:="Export to pdf")
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=UseName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
Save to PDF
A Quick Fix
Sub SavetoPDF()
Const PrintAreaAddress As String = "A1:M500"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
' Store print area in a variable.
Dim SavedPrintArea As String: SavedPrintArea = ws.PageSetup.PrintArea
' Change the print area to the required one.
ws.PageSetup.PrintArea = PrintAreaAddress
Application.ScreenUpdating = False
Dim UseName As Variant
UseName = Application.GetSaveAsFilename( _
InitialFileName:="Report.pdf", _
FileFilter:="PDF files, *.pdf", _
Title:="Export to pdf")
wb.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=UseName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
' Restore previous print area.
ws.PageSetup.PrintArea = SavedPrintArea
ws.Select
Application.ScreenUpdating = True
End Sub

Convert excel columns towards different pdf pages

Below you can find my code that is working for a certain area. But I want to add a new element in the code but I can't find it how I can do it. The first area is A1:E42, but now I have one worksheet where I have 3 area. A1:E42, H1:K42 and O1:R42). How do I need to rewrite my code that the first area comes on the first pdf page, the second area on the second page et cetera.
Sub SavePDF()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
With ws.PageSetup
.Orientation = xlLandscape
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
.PrintArea = "A1:E42"
End With
Set ws = Nothing
Sheets("Offerte_M").ExportAsFixedFormat x1TypePDF, Filename:= _
"C:\Intel\" & ActiveSheet.Range("F21").Value & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
MsgBox "Offerte has been saved as PDF. Press send now."
I thought maybe this was the correct way, but that doesn't work also.
Sub CommandButton1_Click()
Application.ScreenUpdating = False
Offerte_M.PageSetup.PrintArea = "A1:E42"
Offerte_M.PageSetup.PrintArea = "H1:K42"
Offerte_M.PageSetup.PrintArea = "O1:R42"
Worksheets(Array("Offerte_M")).Select.ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\Intel\" & ActiveSheet.Range("F21").Value & ".pdf", _
OpenAfterPublish:=True
Offerte_M.PageSetup.PrintArea = ""
Offerte_M.PageSetup.PrintArea = ""
Offerte_M.PageSetup.PrintArea = ""
Offerte_M.Select
Application.ScreenUpdating = True
End Sub

Reference Pivot table filter

I am trying to reference a PIVOT table filter to use as the Save As name for generating a PDF. I cannot seem to find any combination of PIVOT table object reference to do this.
Sub Deferred_Rent_To_PDF()
Dim strWorksheet As String
Dim strPivotTable As String
Dim pdfFilename As Variant
Dim strDocName As String
Dim ptDeferredRent As pivotTable
strWorksheet = "Deferred"
strPivotTable = "DeferredRent"
Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
'strDocName = ptDeferredRent. <----- THIS IS WHERE I NEED HELP
pdfFilename = Application.GetSaveAsFilename(InitialFileName:=strDocName, _
FileFilter:="PDF, *.pdf", Title:="Save As PDF")
If pdfFilename <> False Then
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=pdfFilename, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End If
End Sub
Try something like this:
You are looking for field_name.CurrentPage but only if field_name.Orientation = xlPageField (i.e. filter field and not row or data or column field or hidden)
Sub Deferred_Rent_To_PDF()
Dim strWorksheet As String
Dim strPivotTable As String
Dim pdfFilename As Variant
Dim strDocName As String
Dim ptDeferredRent As PivotTable
strWorksheet = "Pivot (2)" '"Deferred"
strPivotTable = "PivotTable7" '"DeferredRent"
ThisWorkbook.Sheets(strWorksheet).Activate
Set ptDeferredRent = Worksheets(strWorksheet).PivotTables(strPivotTable)
'strDocName = ptDeferredRent. <----- THIS IS WHERE I NEED HELP
strDocName = Get_Pivot_filter_field(ptDeferredRent)
If strDocName <> "not found" Then
Debug.Print strDocName
pdfFilename = Application.GetSaveAsFilename(InitialFileName:=strDocName, _
FileFilter:="PDF, *.pdf", Title:="Save As PDF")
If pdfFilename <> False Then
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=pdfFilename, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End If
End If
End Sub
Function Get_Pivot_filter_field(pvt As PivotTable)
'On Error Resume Next
Debug.Print pvt.Name
Pivot_Table_Name = pvt.Name
Debug.Print pvt.PivotFields.Count
Get_Pivot_filter_field = "not found"
For Each field_name In pvt.VisibleFields 'pvt.PivotFields
If pivot_field_active(Pivot_Table_Name, field_name) Then
With field_name
Debug.Print field_name & " " & .Orientation
If .Orientation = xlPageField Then 'xlDataField (4)' 'xlColumnField (2)' 'xlHidden (0)' 'xlPageField (3)' 'xlRowField (1)'
Debug.Print field_name & " " & .Orientation & .CurrentPage
Get_Pivot_filter_field = .CurrentPage
Else
Debug.Print field_name & " not filter field"
End If
End With
Else
Debug.Print field_name & " not active"
End If
Next
End Function
Function pivot_field_active(ByVal Pivot_Table_Name As String, ByVal strName As String) As Boolean
Dim strTemp As String
On Error Resume Next
With ActiveSheet.PivotTables(Pivot_Table_Name).PivotFields(strName)
If .NumberFormat = "$#,##0" Then
'Do nothing no error
End If
If .CurrentPage Then
Err.Clear
End If
End With
If Err = 0 Then pivot_field_active = True Else pivot_field_active = False
End Function
What I do here is loop through all pvt.VisibleFields where pvt is the pivot table you pass into the function pvt.VisibleFields(pvt)
If .Orientation = xlPageField then it is a filter field and if it is then return the .CurrentPage as the result of the function Get_Pivot_filter_field otherwise return "not found"
Then use this .CurrentPage as your PDF name.
Full example here: https://drive.google.com/file/d/1HkeJVgKeFeCuj2ItRn2s90ozy41zlCVL/view?usp=sharing

Hide rows if no text on export to PDF

I have a simple script for exporting a range to Excel:
`Private Sub CommandButton2_Click()
With Sheets("Summary").Range("B2:H83")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="F:\Export.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
End Sub
`
The issue is that certain rows may or may not be populated based on inputs in separate sheets. So for example Rows 34-42 could be populated, but more likely than not only Row 34 is populated. This leaves a lot of white space in the exported PDF.
I have no idea how to structure my code to check if there is info in the Row, and if not then to hide the row thereby eliminating white space
I have adjusted your script to hide blank rows. It checks only the range you mention above, in case it's a dynamic range you can let me know.
Private Sub CommandButton2_Click()
Dim rowCounter As Integer
Dim columnCounter As Integer
Dim blankFlag As Boolean
With sheets("Summary")
'hide blank rows in 34-42 and 44-56 interval
For rowCounter = 34 To 56
blankFlag = True
For columnCounter = 2 To 8
If .Cells(rowCounter, columnCounter) <> "" Then
blankFlag = False
Exit For
End If
Next columnCounter
If blankFlag = True Then rows(rowCounter).EntireRow.Hidden = True
If rowCounter = 42 then rowCounter = rowCounter + 1
Next rowCounter
End With
With sheets("Summary").Range("B2:H83")
'export to PDF
.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:="F:\Export.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
End Sub
Better to hide all of the empty rows at once instead of hiding them individually on each iteration of the loop.
Private Sub CommandButton2_Click()
Dim mainRng As Range
Set mainRng = sheets("Summary").Range("B2:H83")
Dim unionRng As Range
Dim roww As Range
For Each roww In mainRng.rows
If WorksheetFunction.CountA(roww) = 0 Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, roww)
Else
Set unionRng = roww
End If
End If
Next
If Not unionRng Is Nothing Then unionRng.EntireRow.Hidden = True
mainRng.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:="F:\Export.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
If Not unionRng Is Nothing Then unionRng.EntireRow.Hidden = False
End Sub
I think you can try this code:
Private Sub CommandButton2_Click()
Dim ShtSummary As Worksheet
Dim i As Integer
Set ShtSummary = Sheets("Summary")
For i = 3 To 83
If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then
ShtSummary.Rows(i).EntireRow.Hidden = True
End If
Next i
With Sheets("Summary").Range("B2:H83")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="F:\Export.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
End Sub
Good Luck.

Avoiding 'Save As' dialog box in Excel VBA script

I have cobbled together a VBA script that loops through a list of data, changing the value of a single cell on a summary page. That cell drives a number of formulas. After each iteration, the cell range of interest is saved off as a PDF.
I am looking to avoid having to manually hit enter every time the 'save as' dialog box is generated on each loop. Once I deploy this script, I could be looking at 1k+ iterations.
Sub AlterID()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
Set ws = Worksheets("Summary Data")
For Each c In Worksheets("Data").Range("A2:A11").Cells
Worksheets("Summary Data").Range("B1").Value = c.Value
strFile = ws.Range("D3").Value
strFile = ThisWorkbook.Path & "\" & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.Range("D3:H9").Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
Next
End Sub
Sub AlterID()
Dim ws As Worksheet, c As Range
Dim strFile As String
Set ws = Worksheets("Summary Data")
For Each c In Worksheets("Data").Range("A2:A11").Cells
ws.Range("B1").Value = c.Value
strFile = ThisWorkbook.Path & "\" & ws.Range("D3").Value
ws.Range("D3:H9").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next
End Sub
Have you tried turning off application alerts?
Application.DisplayAlerts = False 'before your code
Application.DisplayAlerts = True 'after your code
Edit 1
Here is a sub I use to save a file to a PDF
Sub SaveAsPDF()
Dim myValue As Variant
Dim mySheets As Variant
mySheets = Array("Report")
For Each sh In mySheets
Sheets(sh).PageSetup.Orientation = xlLandscape
Next
uid = InputBox("Enter your UID")
uid = StrConv(uid, vbProperCase)
Application.PrintCommunication = False
With Sheets("Report").PageSetup
.FitToPagesWide = 1
.FitToPagesTall = False
End With
Application.PrintCommunication = True
Dim fName As String
fName = "HMB SOX report for week ending " & ActiveSheet.Range("H4").Text
Call selectPage("Report")
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
"C:\Users\" & uid & "\Desktop\" & fName, :=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, Publish:=False
End With
End Sub

Resources