I have a spreadsheet for quotes. I created a macro to save the sheet as a new excel workbook & a PDF simultaneously.
If I only have the file name save as the Quote# + Customer Name, I have no issues.
But, if I want to add on the location name onto the filename it saves as, I am getting a Runtime 91 error.
We do several quotes for the same companies throughout the year, so I want to add the location name to the filename, to easily identify them.
Error message:
Run-time error '91':
Object variable or With block variable not set
VBA Code (example workbook attached):
Sub SaveInvAsPDF()
Dim invyr As String
Dim invno As String
Dim contname As String
Dim custname As String
Dim prepby As String
Dim dt_issue As Date
Dim dt_exp As Date
Dim path As String
Dim fname As String
Dim nextrec As Range
Dim location As Range
invyr = Range("M6")
invno = Range("O6")
contname = Range("E11")
custname = Range("E12")
dt_issue = Range("Q6")
dt_exp = Range("Q8")
prepby = Range("P4")
location = Range("B17")
path = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes"
fname = invyr & invno & "_" & custname & location
savetodirectory = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=path & fname
Set nextrec = Sheet1.Range("A1048576").End(xlUp).Offset(1, 0)
nextrec = invyr
nextrec.Offset(0, 1) = invno
nextrec.Offset(0, 3) = contname
nextrec.Offset(0, 4) = custname
nextrec.Offset(0, 5) = prepby
nextrec.Offset(0, 6) = dt_issue
nextrec.Offset(0, 7) = dt_exp
Sheet1.Hyperlinks.Add anchor:=nextrec.Offset(0, 9), Address:=path & fname & ".pdf"
End Sub
Sub SaveInvAsExcel()
Dim invyr As String
Dim invno As String
Dim contname As String
Dim custname As String
Dim prepby As String
Dim dt_issue As Date
Dim dt_exp As Date
Dim path As String
Dim fname As String
Dim nextrec As Range
Dim location As Range
invyr = Range("M6")
invno = Range("O6")
contname = Range("E11")
custname = Range("E12")
location = Range("B17")
dt_issue = Range("Q6")
dt_exp = Range("Q8")
prepby = Range("P4")
path = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes\Quote Spreadsheets"
fname = invyr & invno & "_" & custname & location
savetodirectory = "S:\OILFIELD\00 QUOTES\001 SMALL QUOTES\2022 Quotes"
'then clear color fill from grey cells
Range("E11,M11,B17:Q17,B20:S34,B37").Interior.Color = xlNone
'copy quote sheet to a new workbook
Sheet3.Copy
'then delete all buttons on worksheet
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If shp.Type <> msoPicture Then shp.Delete
Next shp
'then clear color fill from grey cells
Range("E11,M11,B17:Q17,B20:S34,B37").Interior.Color = xlNone
'save new workbook to specified folder
With ActiveWorkbook
.Sheets(1).Name = "Quote"
.SaveAs Filename:=path & fname, FileFormat:=51
.Close
End With
'close workbook
'save quote as PDF to specified folder
With ActiveWorkSheet
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ignoreprintareas:=False, Filename:=savetodirectory & fname
End With
'need to put quote details in the record of quotes sheet
Set nextrec = Sheet1.Range("A1048576").End(xlUp).Offset(1, 0)
nextrec = invyr
nextrec.Offset(0, 1) = invno
nextrec.Offset(0, 3) = contname
nextrec.Offset(0, 4) = custname
nextrec.Offset(0, 5) = prepby
nextrec.Offset(0, 6) = dt_issue
nextrec.Offset(0, 7) = dt_exp
'add hyperlink to Excel & PDF files in the record of quotes sheet
With Worksheets(1)
Sheet1.Hyperlinks.Add anchor:=nextrec.Offset(0, 10), Address:=path & fname & ".xlsx", TextToDisplay:="Excel Copy"
Sheet1.Hyperlinks.Add anchor:=nextrec.Offset(0, 9), Address:=savetodirectory & fname & ".pdf", TextToDisplay:="PDF Copy"
End With
End Sub
Sub CreateNewInvoice()
Dim invno As Long
invno = Range("O6")
Range("E11,P4,M11,B17,G17,Q17,B20:S34,B37").Value = ""
MsgBox "Your next quote number is " & invno + 1
Range("O6") = invno + 1
Range("E11").Select
Range("P4,E11,M11,B17,G17,Q17,B20:S34,B37").Interior.Color = RGB(242, 242, 242)
ThisWorkbook.Save
End Sub
Sub RecordofInvoice()
Dim invyr As String
Dim invno As String
Dim contname As String
Dim custname As String
Dim prepby As String
Dim dt_issue As Date
Dim dt_exp As Date
Dim nextrec As Range
invyr = Range("M6")
invno = Range("O6")
contname = Range("E11")
custname = Range("E12")
dt_issue = Range("Q6")
dt_exp = Range("Q8")
prepby = Range("P4")
Set nextrec = Sheet1.Range("A1048576").End(xlUp).Offset(1, 0)
nextrec = invyr
nextrec.Offset(0, 1) = invno
nextrec.Offset(0, 3) = contname
nextrec.Offset(0, 4) = custname
nextrec.Offset(0, 5) = prepby
nextrec.Offset(0, 6) = dt_issue
nextrec.Offset(0, 7) = dt_exp
End Sub
Bookmarks