VBA User Form Greater than value not calculating correctly

Histteacher

New Member
Joined
Jul 30, 2015
Messages
2
Excel 2013

Issue: the first textbox value is compared to if it is greater than the total value of textbox 2. The issue is, only the first number (left to right) of each box is evaluated...likely reading it as a text.

Suggestions?
The closest posting I have found is here: http://www.mrexcel.com/forum/excel-questions/311520-visual-basic-applications-interesting-problem-100-not-greater-than-39-a.html


Code:
Private Sub CommandButton1_Click()
    Dim NextRow As Long


    'Make sure Sheet1 is active
    Sheets("Sheet1").Activate

'Make sure Objective 1 Score is entered as number
    If IsNumeric(tbObj1_Score.Value) Or tbObj1_Score.Value = "" Then
        tbObj1_Score.Value = tbObj1_Score.Value
        Else
        MsgBox "You must enter a numeric value for the score."
        tbObj1_Score.SetFocus
        Exit Sub
    End If
    
    'Make sure Objective 1 Total is entered as number
    If IsNumeric(tbObj1_Total.Value) Or tbObj1_Total.Value = "" Then
        tbObj1_Total.Value = tbObj1_Total.Value
        Else
        MsgBox "You must enter a numeric value for the score."
        tbObj1_Total.SetFocus
        Exit Sub
    End If
    
    
    'Here is my error!!! VBA appears to only read the first number of each object,
    'therefore it shows an error message for 8 > 10.
    'Make sure Objective 1 Total Points is entered if there is a score in Obj 1 Score
    If tbObj1_Score.Value > tbObj1_Total.Value = True Then
    MsgBox "2222   Total possible points as a numeric value greater than the possible points."
        tbObj1_Total.SetFocus
        Exit Sub
    End If
    
    
    
        'Determine the next empty row
    NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
    
    'Transfer Objective 1 Score
    Cells(NextRow, 1) = tbObj1_Score.Value
    
    'Transfer Objective 1 Total Points
    Cells(NextRow, 2) = tbObj1_Total.Value
    
    'Unload and close
    Unload UserForm1
    Sheets("Sheet1").Select
    

End Sub
 
It makes sense that it's doing a text comparison - your best answer is to put the textbox values into integer (or whatevers most appropriate) variables before comparing them.

Otherwise you can wrap them with CInt() in the comparison.
 
Upvote 0

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