Do not show type mismatch message when clicking cancel on Msgbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,843
Office Version
  1. 2007
Platform
  1. Windows
I have a code shown below where the user enters a row number & is then taken to it.
Somethimes the user will click on Cancel ot the red cross to exit BUT the Type mismatch message is shown.

What do i need to add to the code so in future if the message is not shwon.
Thanks

VBA Code:
Private Sub GoToRow_Click()
   Dim lnRow As Long
    lnRow = InputBox("ENTER ROW NUMBER.", "GO TO ROW NUMBER")
    Cells(lnRow, 1).Select
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I have a code shown below where the user enters a row number & is then taken to it.
Somethimes the user will click on Cancel ot the red cross to exit BUT the Type mismatch message is shown.

What do i need to add to the code so in future if the message is not shwon.
Thanks

VBA Code:
Private Sub GoToRow_Click()
   Dim lnRow As Long
    lnRow = InputBox("ENTER ROW NUMBER.", "GO TO ROW NUMBER")
    Cells(lnRow, 1).Select
End Sub

A change to the variable type to Variant instead of Long will sort it.

VBA Code:
Private Sub GoToRow_Click()
Dim lnRow As Variant

    lnRow = InputBox("ENTER ROW NUMBER.", "GO TO ROW NUMBER")
    
    If lnRow <> "" Then
      Cells(lnRow, 1).Select
    End If
    
End Sub
 
Upvote 0
Solution
I would also check for anything else then a number like letters:

VBA Code:
Private Sub GoToRow_Click()
   Dim lnRow As Variant
    lnRow = InputBox("ENTER ROW NUMBER.", "GO TO ROW NUMBER")
    If (lnRow = 0 Or lnRow = vbNullString) Then Exit Sub
    If (VarType(lnRow) <> vbInteger Or VarType(lnRow) <> vbLong) Then Exit Sub
    Cells(lnRow, 1).Select
End Sub
 
Upvote 0
That doesnt take me to a row after i click oK or hit enter
 
Upvote 0
oops, my fault. Try this:

VBA Code:
Private Sub GoToRow_Click()
   Dim lnRow As Variant
    lnRow = InputBox("ENTER ROW NUMBER.", "GO TO ROW NUMBER")
    If (lnRow = 0 Or lnRow = vbNullString) Then Exit Sub
    If IsNumeric(lnRow) Then
      lnRow = CLng(lnRow)
    Else
      Exit Sub
    End If
    Cells(lnRow, 1).Select
End Sub
 
Upvote 0
That doesnt take me to a row after i click oK or hit enter

try following & see if resolves

VBA Code:
Private Sub GoToRow_Click()
    Dim lnRow       As Variant
    Do
        lnRow = InputBox("ENTER ROW NUMBER.", "GO To ROW NUMBER")
        'cancel pressed
        If StrPtr(lnRow) = 0 Then Exit Sub
        lnRow = Val(lnRow)
    Loop Until lnRow > 0
 
    Cells(CLng(lnRow), 1).Select

End Sub

Solution should ignore any non numeric value entered

Dave
 
Upvote 0
That worked thanks
Post #6
You could also use the Application.InputBox which displays a message if the entry is incorrect.

Just see which one you like the best.

VBA Code:
Private Sub GoToRow_Click()
Dim lnRow As Variant

  ' Turn off errors.
  On Error Resume Next
    
  lnRow = Application.InputBox("Enter number", Type:=1) ' The type 1 is a number.
    
  ' Turn on errors.
  On Error GoTo 0
    
  If lnRow <> False Then
    Cells(lnRow, 1).Select
  End If
  
End Sub
 
Upvote 0
I will do that.
So if you want to suggest one more then i will also look at that.

Lets say my last row with values is 2345

If a user enters a row number greater than that also show the Msgbox.
After all we should only be taken to a row with values in.

Thanks
 
Upvote 0

Forum statistics

Threads
1,225,626
Messages
6,186,090
Members
453,337
Latest member
fiaz ahmad

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