Limit an InputBox to just 2 numbers

grady121

Active Member
Joined
May 27, 2005
Messages
383
Office Version
  1. 2016
Platform
  1. Windows
On my Golf Handicap workbook and using the golfer’s names listed in column B, I use an InputBox as a way to enter each players scores in turn, onto the current worksheet in 2 different columns. (L & AM)

I need to limit the score I can enter in the InputBox to just 2 numbers (i.e. 99 or less). Can anyone suggest any additions to my part-code that will meet my needs?

Code:
 Sub AddScore()
 
    Dim ListRow As Integer, ListColumn As Integer, NewDataColumn As Integer, NewDataColumn2 As Integer
    Dim MyNewData As String
    Dim iRet As Integer

    
    ' Using Names in Columns B, Add new Scores to Columns L & AM
    Application.ScreenUpdating = True
    ListRow = 10: ListColumn = 2: NewDataColumn = 12: NewDataColumn2 = 39 '(Columns B, L & AM)
    While ActiveSheet.Cells(ListRow, ListColumn) <> ""
        MyNewData = InputBox("Enter the Scores for:- " & vbNewLine & vbNewLine _
        & ActiveSheet.Cells(ListRow, ListColumn) & vbNewLine & vbNewLine & vbNewLine _
        & "Click 'OK' or press 'Enter' to add next players score." & vbNewLine & vbNewLine _
        & "(If a Player did not play - click 'OK' or press 'Enter')", _
        "Add Scores", ActiveSheet.Cells(ListRow, NewDataColumn))    'Get input
        If MyNewData <> "" Then
            ActiveSheet.Cells(ListRow, NewDataColumn) = MyNewData  'If input is not empty, use the input
            ActiveSheet.Cells(ListRow, NewDataColumn2) = MyNewData
        Else: ActiveSheet.Cells(ListRow, NewDataColumn) = ""
        End If
        ListRow = ListRow + 1
    Wend
    
End Sub



Any help appreciated.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Maybe
Code:
...
Do
    MyNewData = InputBox("Enter the Scores for:- " & vbNewLine & vbNewLine _
    & ActiveSheet.Cells(ListRow, ListColumn) & vbNewLine & vbNewLine & vbNewLine _
    & "Click 'OK' or press 'Enter' to add next players score." & vbNewLine & vbNewLine _
    & "(If a Player did not play - click 'OK' or press 'Enter')", _
    "Add Scores", ActiveSheet.Cells(ListRow, NewDataColumn))    'Get input
Loop Until (MyNewData Like "##") Or (MyNewData = "")
...
 
Upvote 0
Thanks for your quick response Tetra201.
I've modified my code as below and it works well, but I can't get it to move on to the next player, if I enter just a single number.
Some of our players are good at ruining a good walk !! LOL
Code:
Code:
    While ActiveSheet.Cells(ListRow, ListColumn) <> ""
    Do
    MyNewData = InputBox("Enter the Scores for:- " & vbNewLine & vbNewLine _
    & ActiveSheet.Cells(ListRow, ListColumn) & vbNewLine & vbNewLine & vbNewLine _
    & "Click 'OK' or press 'Enter' to add next players score." & vbNewLine & vbNewLine _
    & "(If a Player did not play - click 'OK' or press 'Enter')", _
    "Add Scores", ActiveSheet.Cells(ListRow, NewDataColumn))    'Get input
    If Len(MyNewData) > 2 Then MsgBox "The score for:- '" & ActiveSheet.Cells(ListRow, ListColumn) & "' is too high." & vbCrLf & vbCrLf _
             & "Max of 2 Numbers ! ... Please re-enter score.", vbOKOnly + vbCritical, "Score Error"
    Loop Until (MyNewData = "#") Or (MyNewData Like "##") Or (MyNewData = "")
    
    If MyNewData <> "" Then
            ActiveSheet.Cells(ListRow, NewDataColumn) = MyNewData  'If input is not empty, use the input
            ActiveSheet.Cells(ListRow, NewDataColumn2) = MyNewData
        Else: ActiveSheet.Cells(ListRow, NewDataColumn) = ""
        End If
        ListRow = ListRow + 1
    Wend

I've tried adding (MyNewData = "#"), but do you have any ideas.
 
Upvote 0
Maybe like this.

Howard

Code:
Option Explicit
 Sub AddScore_Two()
 
    Dim ListRow As Integer, ListColumn As Integer, NewDataColumn As Integer, NewDataColumn2 As Integer
    Dim MyNewData As String
    Dim iRet As Integer
    
    ' Using Names in Columns B, Add new Scores to Columns L & AM
  '  Application.ScreenUpdating = True
    ListRow = 10: ListColumn = 2: NewDataColumn = 12: NewDataColumn2 = 39 '(Columns B, L & AM)
    While ActiveSheet.Cells(ListRow, ListColumn) <> ""
nTwo:
        MyNewData = InputBox("Enter the Scores for:- " & vbNewLine & vbNewLine _
        & ActiveSheet.Cells(ListRow, ListColumn) & vbNewLine & vbNewLine & vbNewLine _
        & "Click 'OK' or press 'Enter' to add next players score." & vbNewLine & vbNewLine _
        & "(If a Player did not play - click 'OK' or press 'Enter')", _
        "Add Scores", ActiveSheet.Cells(ListRow, NewDataColumn))    'Get input
        
       If Len(MyNewData) > 2 Then
            MsgBox "Invalid entry"
            GoTo nTwo
          Else
           MsgBox "You entered:" & MyNewData
        
        If MyNewData <> "" Then
            ActiveSheet.Cells(ListRow, NewDataColumn) = MyNewData  'If input is not empty, use the input
            ActiveSheet.Cells(ListRow, NewDataColumn2) = MyNewData
          Else: ActiveSheet.Cells(ListRow, NewDataColumn) = ""
        End If
        
       End If
        ListRow = ListRow + 1
    Wend
    
End Sub
 
Upvote 0
Thanks for your quick response Tetra201.
I've modified my code as below and it works well, but I can't get it to move on to the next player, if I enter just a single number.
Some of our players are good at ruining a good walk !! LOL
Code:
Rich (BB code):
    While ActiveSheet.Cells(ListRow, ListColumn) <> ""
    Do
    MyNewData = InputBox("Enter the Scores for:- " & vbNewLine & vbNewLine _
    & ActiveSheet.Cells(ListRow, ListColumn) & vbNewLine & vbNewLine & vbNewLine _
    & "Click 'OK' or press 'Enter' to add next players score." & vbNewLine & vbNewLine _
    & "(If a Player did not play - click 'OK' or press 'Enter')", _
    "Add Scores", ActiveSheet.Cells(ListRow, NewDataColumn))    'Get input
    If Len(MyNewData) > 2 Then MsgBox "The score for:- '" & ActiveSheet.Cells(ListRow, ListColumn) & "' is too high." & vbCrLf & vbCrLf _
             & "Max of 2 Numbers ! ... Please re-enter score.", vbOKOnly + vbCritical, "Score Error"
    Loop Until (MyNewData = "#") Or (MyNewData Like "##") Or (MyNewData = "")
    
    If MyNewData <> "" Then
            ActiveSheet.Cells(ListRow, NewDataColumn) = MyNewData  'If input is not empty, use the input
            ActiveSheet.Cells(ListRow, NewDataColumn2) = MyNewData
        Else: ActiveSheet.Cells(ListRow, NewDataColumn) = ""
        End If
        ListRow = ListRow + 1
    Wend

I've tried adding (MyNewData = "#"), but do you have any ideas.
Good catch!

Try:
Code:
Loop Until (MyNewData [COLOR=#ff0000]Like[/COLOR] "#") Or (MyNewData Like "##") Or (MyNewData = "")
 
Upvote 0
Thanks Tetra201, that will do nicely...
Works a dream.
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,604
Members
449,109
Latest member
Sebas8956

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