Display different message box based on cell value

DanGK1

Board Regular
Joined
Jul 7, 2010
Messages
60
Hello

I have a workbook that can open a URL into IE when a project number is entered in an input box.

What I would like to happen is that if the project number entered does not have a URL against it and the value is #N/A, then the message box will display "Project does not exist". However if the number does exist, I want a different message box to be displayed which is a yes/no box.

The code below is what I have tried which works fine if the cell value is #N/A but when it is not it throws an Run-Time error 13: Type mismatch

Code:
Sub SearchPidsMessageBox()
Dim Answer As String
Dim MyNote As String
If CVErr(Range("o2").Value) = CVErr(xlErrNA) Then
  MsgBox "PID PR" & Range("n2").Value & " Does not exist", , "Project ID not found"
ElseIf CVErr(Range("o2").Value) <> CVErr(xlErrNA) Then
'Else
'I have tried ElseIF and just Else and both give same error
    'Place your text here
    MyNote = "PID PR" & Range("n2").Value & " not allocated. Open URL?"
    'Display MessageBox
    Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Project ID Not Found")
    If Answer = vbNo Then
        'Code for No button Press
    Else
        'Code for Yes button Press
        Call LoadWebPage
    End If
End If
End Sub

Any help greatly appreciated.

Thanks

Dan
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The following code appears to work as you request.

The issue was that the CVErr() function takes an error number as its argument and when Range("O2").Value isn't an error it returns something else (I assume text) which is a type mismatch (string instead of Number) to what the function is expecting.

Code:
Sub SearchPidsMessageBox()
Dim Answer As String
Dim MyNote As String
    If IsError(Range("o2").Value) Then
      MsgBox "PID PR" & Range("n2").Value & " Does not exist", , "Project ID not found"
    Else
    'I have tried ElseIF and just Else and both give same error
        'Place your text here
        MyNote = "PID PR" & Range("n2").Value & " not allocated. Open URL?"
        'Display MessageBox
        Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Project ID Not Found")
        If Answer = vbNo Then
            'Code for No button Press
        Else
            'Code for Yes button Press
            Call LoadWebPage
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,535
Members
449,169
Latest member
mm424

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