Do not show type mismatch message when clicking cancel on Msgbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
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
 
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
You could do this.

It continues to ask for the row number until it is <=2345.

Cancel will escape the loop.

No error message is displayed.

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

  ' Turn off errors.
  On Error Resume Next
  
  Do
  
    lnRow = Application.InputBox("Enter number", Type:=1) ' The type is number
      
    ' Turn on errors.
    On Error GoTo 0
      
    If lnRow = False Then
      Exit Sub
    End If
  
  Loop Until lnRow <= 2345
  
  Cells(lnRow, 1).Select
    
End Sub
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The row value will increase each day, 2345 was an example of what it currently is.

Also just clicking on OK pops up error message but doesnty say what it is.

I will just use one of the previous advised from above thanks
 
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
Or this where a message appears and the last row is hard coded. Not very flexible.

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

  ' Turn off errors.
  On Error Resume Next
  
  Do
  
    lnRow = Application.InputBox("Enter number", Type:=1) ' The type is number
      
    ' Turn on errors.
    On Error GoTo 0
      
    If lnRow = False Then
      Exit Sub
    End If
    
    If lnRow > 2345 Then
      MsgBox "The number that you enter should be less than or equal to 2345.", vbOKOnly, "Warning!"
    End If
  
  Loop Until lnRow <= 2345
  
  Cells(lnRow, 1).Select
    
End Sub
 
Upvote 0
The row value will increase each day, 2345 was an example of what it currently is.

Also just clicking on OK pops up error message but doesnty say what it is.

I will just use one of the previous advised from above thanks
Or this where it does not let entry more than the last row on the active sheet.

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

  lngLastRow = ActiveSheet.UsedRange.Rows.Count
  
  ' Turn off errors.
  On Error Resume Next
  
  Do
  
    lnRow = Application.InputBox("Enter number", Type:=1) ' The type is number.
      
    ' Turn on errors.
    On Error GoTo 0
      
    If lnRow = False Then
      Exit Sub
    End If
    
    If lnRow > lngLastRow Then
      MsgBox "The number that you enter should be less than or equal to " & lngLastRow & ".", vbOKOnly, "Warning!"
    End If
  
  Loop Until lnRow <= lngLastRow
  
  Cells(lnRow, 1).Select
    
End Sub
 
Upvote 0
I will leave it thanks.
Im just seeing more & more odd messages

EaseUS_2024_12_ 1_14_34_33.jpg


EaseUS_2024_12_ 1_14_34_43.jpg
 
Upvote 0
I will leave it thanks.
Im just seeing more & more odd messages

If you want to stay with your original InputBox.Function approach should be able to avoid need for additional prompts

see if this update is of any help to you.

VBA Code:
Private Sub GoToRow_Click()
    Dim lnRow       As Variant
    Dim LastRow     As Long
    Dim strDefault As String
   
    Const ErrPrompt As String = Const ErrPrompt As String = "The Number Must Be > 0 And Less Or Equal To "
   
    LastRow = ActiveSheet.UsedRange.Rows.Count

    Do
        lnRow = InputBox("ENTER ROW NUMBER.", "GO To ROW NUMBER", strDefault)
        'cancel pressed
        If StrPtr(lnRow) = 0 Then Exit Sub
        lnRow = Val(lnRow)
        strDefault = ErrPrompt & LastRow
    Loop Until lnRow > 0 And lnRow <= LastRow
   
    Cells(CLng(lnRow), 1).Select
End Sub

Dave
 
Upvote 0
If you want to stay with your original InputBox.Function approach should be able to avoid need for additional prompts

see if this update is of any help to you.

VBA Code:
Private Sub GoToRow_Click()
    Dim lnRow       As Variant
    Dim LastRow     As Long
    Dim strDefault As String
  
    Const ErrPrompt As String = Const ErrPrompt As String = "The Number Must Be > 0 And Less Or Equal To "
  
    LastRow = ActiveSheet.UsedRange.Rows.Count

    Do
        lnRow = InputBox("ENTER ROW NUMBER.", "GO To ROW NUMBER", strDefault)
        'cancel pressed
        If StrPtr(lnRow) = 0 Then Exit Sub
        lnRow = Val(lnRow)
        strDefault = ErrPrompt & LastRow
    Loop Until lnRow > 0 And lnRow <= LastRow
  
    Cells(CLng(lnRow), 1).Select
End Sub

Dave

So many ways to do the same job.

Line of code should read:

VBA Code:
Const ErrPrompt As String = "The Number Must Be > 0 And Less Or Equal To "
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,708
Members
452,994
Latest member
Janick

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