I cannot figure out what is wrong with this code. I've run it on one workbook and it worked like it always does. Then I run it on the one I need and it fails after seven exports. I even moved the eight sheet to the front to see if that was causing it to fail. Nope. It has something to do with the filenames of about 13 of them. I can't provide those, they are personal info, but I am hoping someone might point me in the right direction for why I am getting "invalid procedure call or argument" error (see bolded part in code below). I can't do an On Error Resume Next, because it puts Exported next to the file that would have otherwise stopped to exports. It needs to say the Not exported, then I can troubleshoot.
'Export Notepads as Pdfs and Save with correct Filenames
Option Explicit
Sub ED5ExportNotepadPDFFilename()
Dim wsLIST As Worksheet, ws As Worksheet, fPATH As String, Errors As Boolean
Dim fileLIST As Range, fNAME As Range, MyArr As Variant
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = ActiveWorkbook.Path
.Show
If .SelectedItems.Count > 0 Then
fPATH = .SelectedItems(1) & "\"
Else
MsgBox "No destination selected, aborting..."
Exit Sub
End If
End With
Set wsLIST = ActiveWorkbook.ActiveSheet
Set fileLIST = wsLIST.Range("A:A").SpecialCells(xlConstants)
fileLIST.Offset(, 1).ClearContents
For Each fNAME In fileLIST
MyArr = Split(fNAME.Value, ", ")
For Each ws In ActiveWorkbook.Worksheets
If InStr(ws.Range("A1"), MyArr(2)) > 0 Then
If InStr(ws.Range("B1"), MyArr(0)) > 0 Then
If InStr(ws.Range("B1"), MyArr(1)) > 0 Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=fPATH & fNAME.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
fNAME.Offset(, 1).Value = "exported"
Exit For
End If
End If
End If
If ws.Index = ActiveWorkbook.Sheets.Count Then
fNAME.Offset(, 1).Value = "NOT FOUND"
Errors = True
End If
Next ws
Next fNAME
Columns("B:B").EntireColumn.AutoFit
Columns("B:B").Select
ActiveWorkbook.Worksheets("Sheet31").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet31").Sort.SortFields.Add Key:=Range("B1"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet31").Sort
.SetRange Range("A2:B219")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
If Errors Then MsgBox "Not all filenames/sheets were matched. See FILENAMES column B"
End Sub
Bookmarks