This is driving me mental
The code below is supposed to loop through a list of customers on ActiveSummary sheet, copy the account number into G9 on ActiveFrontSheet and then save ActiveFrontSheet as a pdf in a specified file. The file path for the main folder is in ActiveSummary B1 and the subfolder is in ActiveSummary C7 so that I can change the file path if I want to without having to edit the code.
When I run the macro it gives me "runtime error 1004: Document not saved. The file may be open, or an error may have been encountered when saving" Debug takes it to here:PHP Code:
Sub Print_Active()
Dim ActiveSummary As Worksheet
Set ActiveSummary = Worksheets("ActiveSummary")
' Sort ActiveSummery by Customer Name
With ActiveSummary
Dim lastrow As Long
lastrow = .Cells(Rows.Count, 2).End(xlUp).row
.Range("A10:J" & lastrow).Sort key1:=.Range("B10:B" & lastrow), _
order1:=xlAscending, Header:=xlNo
End With
'Copy account number and past into ActiveFrontSheet
Sheets("ActiveSummary").Select
Range("A10").Select
Fpath = Range("B1").Text & Range("C7") & "\"
While ActiveCell.Value > "1"
Selection.Copy
Sheets("ActiveFrontSheet").Select
Range("G9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
' Build File name and save as .pdf
Fname = "#Front Sheet" & UCase(Range("G12").Value & " (" & Range("G9") & ")")
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fpath & Fname & ".pdf"
Sheets("ActiveSummary").Select
ActiveCell.Offset(1, 0).Select
Wend
End Sub
I have pretty much exactly the same code in a different workbook and it works fine, all i changed when I copied it over to this workbook was the Fname and the Fpath so I don't get why it's not working.PHP Code:
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fpath & Fname & ".pdf"
Can anyone tell me where I'm going wrong please?
Thanks
Soph
Bookmarks