My code doesnt advise which file is missing when Msgbox is shown

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I am using the code below but one part doesnt work correctly & i am unable to see the reason why.

I run the code & i then see a Msgbox pop up saying "PDF DOES NOT EXIST"
I am not told which PDF its refering to,can you see an issue in the code which would cause this.

Obviously it should say something like PDF 123 DOES NOT EXIST

Thanks


Rich (BB code):
Private Sub HyperlinkAllInvoiceNumbers_Click()
    Dim lastRow As Long, myPath As String, fileName As String, fName As Variant
    myPath = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\" 'FOLDER LOCATION OF WHERE PDF ARE SAVED
    lastRow = Range("P" & Rows.Count).End(xlUp).Row
    For i = 6 To lastRow
        If Range("P" & i) <> "N/A" Then
            If Not Dir(myPath & Range("P" & i) & ".pdf", vbDirectory) = vbNullString Then
                ActiveSheet.Hyperlinks.Add Range("P" & i), myPath & Range("P" & i) & ".pdf"
            Else
                MsgBox "PDF " & Range("P" & i) & " DOES NOT EXIST.", vbExclamation, "PDF IS MISSING IN THE FOLDER"
            End If
        End If
    Next i
    MsgBox "ALL HYPERLINKS ARE NOW COMPLETE.", vbInformation, "HYPERLINKING THE INVOICE NUMBER"
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this:

VBA Code:
Private Sub HyperlinkAllInvoiceNumbers_Click()
  Dim lastRow As Long, myPath As String, fileName As String, fName As Variant
  Dim i As Long
  myPath = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\" 'FOLDER LOCATION OF WHERE PDF ARE SAVED
  lastRow = Range("P" & Rows.Count).End(xlUp).Row
  For i = 6 To lastRow
    fileName = Range("P" & i).Value
    If fileName <> "N/A" Then
      If Not Dir(myPath & fileName & ".pdf", vbDirectory) = vbNullString Then
        ActiveSheet.Hyperlinks.Add Range("P" & i), myPath & fileName & ".pdf"
      Else
        If fileName = "" Then fileName = "The cell is empty"
        MsgBox "PDF " & fileName & " DOES NOT EXIST.", vbExclamation, "PDF IS MISSING IN THE FOLDER"
      End If
    End If
  Next i
  MsgBox "ALL HYPERLINKS ARE NOW COMPLETE.", vbInformation, "HYPERLINKING THE INVOICE NUMBER"
End Sub
 
Upvote 0
Hi,
That works better BUT i see this message PDF The cell is empty Does not exist.

When the message is shown can we have it also show the row number please.

Ive looked down the list & dont see an empty cell.
This would track it down should my eyes have failed me.

Thanks
 
Upvote 0
Try:

VBA Code:
Private Sub HyperlinkAllInvoiceNumbers_Click()
  Dim lastRow As Long, myPath As String, fileName As String, fName As Variant
  Dim i As Long
  myPath = "C:\Users\Ian\Desktop\REMOTES ETC\DR\DR COPY INVOICES\" 'FOLDER LOCATION OF WHERE PDF ARE SAVED
  lastRow = Range("P" & Rows.Count).End(xlUp).Row
  For i = 6 To lastRow
    fileName = Range("P" & i).Value
    If fileName <> "N/A" Then
      If Not Dir(myPath & fileName & ".pdf", vbDirectory) = vbNullString Then
        ActiveSheet.Hyperlinks.Add Range("P" & i), myPath & fileName & ".pdf"
      Else
        If fileName = "" Then
          MsgBox "The cell is empty in the row : " & i
        Else
          MsgBox "PDF " & fileName & " DOES NOT EXIST. Row: " & i, vbExclamation, "PDF IS MISSING IN THE FOLDER"
        End If
      End If
    End If
  Next i
  MsgBox "ALL HYPERLINKS ARE NOW COMPLETE.", vbInformation, "HYPERLINKING THE INVOICE NUMBER"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,349
Messages
6,124,427
Members
449,158
Latest member
burk0007

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