Limit an InputBox value to 40 or show Message Box warning

grady121

Active Member
Joined
May 27, 2005
Messages
385
Office Version
  1. 2016
Platform
  1. Windows
With help from this forum I was able to obtain the following code.
An InputBox is used to enter a Player golf score in column L, for each Player listed in column B.

It includes a Message box catch that only appears if the score is above 99 (3 numerical numbers), then re-shows the Players name enabling me to re-enter the score.

What I would prefer now is a code that will replaces the existing catch with a new one, based on any score entered over 40.

Ideally, just a simple Yes/No Message box asking: “Are you sure the score entered is correct?”

If Yes is clicked, then carry on and insert the score. Or No, to re-enter the Players correct score.

Any help appreciated.

Code:
Sub Add_Score()
 
    Dim ListRow, ListColumn, ScoreColumn As Integer
    Dim MyNewScore As String
    
    ' Using Names listed in Column 2 (Starting at Row B10), Add new Scores to Column 12 (L)
    Application.ScreenUpdating = True
    ListRow = 10: ListColumn = 2: ScoreColumn = 12
    
    While ActiveSheet.Cells(ListRow, ListColumn) <> "" ' Until names run out
        Do
            MyNewScore = 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, ScoreColumn))
            
            ' Limit the score to 1 or 2 numbers or ""
            If Len(MyNewScore) > 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 (MyNewScore Like "#") Or (MyNewScore Like "##") Or (MyNewScore = "")
    
        If MyNewScore <> "" Then
            ActiveSheet.Cells(ListRow, ScoreColumn) = MyNewScore  'If input is not empty, use the input
        Else: ActiveSheet.Cells(ListRow, ScoreColumn) = ""
        End If
        ListRow = ListRow + 1
    Wend
    
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try

Code:
Sub Add_Score()
 
    Dim ListRow, ListColumn, ScoreColumn As Integer
    Dim MyNewScore As String
    
    ' Using Names listed in Column 2 (Starting at Row B10), Add new Scores to Column 12 (L)
    Application.ScreenUpdating = True
    ListRow = 10: ListColumn = 2: ScoreColumn = 12
    
    While ActiveSheet.Cells(ListRow, ListColumn) <> "" ' Until names run out
        Do
            MyNewScore = 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, ScoreColumn))
            
            ' Limit the score to 1 or 2 numbers or ""
            If MyNewScore > 40 Then MsgBox "Are you sure the score for:- '" & ActiveSheet.Cells(ListRow, ListColumn) & "' is correct ??" & vbCrLf & vbCrLf _
                     & "Score must be 40 or less ! ... Please re-enter score.", vbOKOnly + vbCritical, "Score Error"
        Loop Until (MyNewScore Like "#") Or (MyNewScore Like "##") Or (MyNewScore = "")
    
        If MyNewScore <> "" Then
            ActiveSheet.Cells(ListRow, ScoreColumn) = MyNewScore  'If input is not empty, use the input
        Else: ActiveSheet.Cells(ListRow, ScoreColumn) = ""
        End If
        ListRow = ListRow + 1
    Wend
    
End Sub
 
Upvote 0
Sorry, typo in last post....try

Code:
Sub Add_Score()
 
    Dim ListRow, ListColumn, ScoreColumn As Integer
    Dim MyNewScore As String
    
    ' Using Names listed in Column 2 (Starting at Row B10), Add new Scores to Column 12 (L)
    Application.ScreenUpdating = True
    ListRow = 10: ListColumn = 2: ScoreColumn = 12
    
    While ActiveSheet.Cells(ListRow, ListColumn) <> "" ' Until names run out
        Do
            MyNewScore = 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, ScoreColumn))
            
            ' Limit the score to 1 or 2 numbers or ""
            If MyNewScore > 40 Then MsgBox "Are you sure the score for:- '" & ActiveSheet.Cells(ListRow, ListColumn) & "' is correct ??" & vbCrLf & vbCrLf _
                     & "Score must be 40 or less ! ... Please re-enter score.", vbOKOnly + vbCritical, "Score Error"
        Loop Until (MyNewScore <= 40) Or (MyNewScore = "")
    
        If MyNewScore <> "" Then
            ActiveSheet.Cells(ListRow, ScoreColumn) = MyNewScore  'If input is not empty, use the input
        Else: ActiveSheet.Cells(ListRow, ScoreColumn) = ""
        End If
        ListRow = ListRow + 1
    Wend
    
End Sub
 
Upvote 0
Thanks Michael,

I'm not hot on Loops but managed to come up with the following code, it seems to do what I need:
Code:
Sub Add_Score()
 
    Dim ListRow, ListColumn, ScoreColumn, iRet As Integer
    Dim MyNewScore As String
    
    ' Using Names listed in Column 2 (Starting at Row B10), Add new Scores to Column 12 (L)
    Application.ScreenUpdating = True
    ListRow = 10: ListColumn = 2: ScoreColumn = 12
    
    While ActiveSheet.Cells(ListRow, ListColumn) <> "" ' Until names run out
        Do
            MyNewScore = 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, ScoreColumn))
            
            If MyNewScore = "" Then GoTo SCORE_OK
            
            ' Warn if score is over 40
            If MyNewScore > 40 Then iRet = _
                    MsgBox(ActiveSheet.Cells(ListRow, ListColumn) & " scored:- " _
                    & MyNewScore & ", is that correct ???" & vbCrLf & vbCrLf _
                    & "If not, just click 'No' to re-enter the score.", vbYesNo + vbQuestion, "Score Check")
            If iRet = vbYes Then GoTo SCORE_OK
        Loop Until (MyNewScore <= 40) Or (MyNewScore = "")
SCORE_OK:

        If MyNewScore <> "" Then
            ActiveSheet.Cells(ListRow, ScoreColumn) = MyNewScore  'If input is not empty, use the input
        Else: ActiveSheet.Cells(ListRow, ScoreColumn) = ""
        End If
        ListRow = ListRow + 1 ' Move to next Row
    Wend
    
End Sub

Thanks again for your suggestion
 
Upvote 0

Forum statistics

Threads
1,224,746
Messages
6,180,705
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