Solved

Excel VBA-  Using Comparison Operators to Prompt Warning Message

Posted on 2013-01-13
4
339 Views
Last Modified: 2013-01-13
Hi Experts,

I have a text box that holds values of adjusted amounts less than $150.  

If the amount entered by the end user is over $150 then a warning message advising the end user to check their entry should generate.  

What's happening with my current code is that it's not recognizing any amounts under $99.00 so the warning message pops warning that the amount entered needs to be under $150 when it is.

It'll accept some entries like $12 and $15 but if I enter $79 or $9.99 it'll trigger the warning box to pop when the entries are clearly under $150.

I need assistance modifying the code so that it'll work properly.

Thank you in Advance for your time.
Sample-Workbook.xlsm
0
Comment
Question by:"Abys" Wallace
  • 2
4 Comments
 
LVL 39

Expert Comment

by:als315
ID: 38772417
You are converting number to text before compare. Remove Format and compare value. Something like this:
Private Sub textAdjust_Exit(ByVal Cancel As MSForms.ReturnBoolean)

'Me.textAdjust.Value = Format(Me.textAdjust.Value, "$#,###,###.00")

'checks to see if adj amount is less than $150
If Me.textAdjust.Value > "150" Then
    MsgBox "Adjustment amount should be $150 or Less.  Please check the amount entered."
    Me.textAdjust.Value = ""
End If

End Sub

Open in new window

If you need text in this field, convert it after IF
0
 
LVL 26

Accepted Solution

by:
redmondb earned 350 total points
ID: 38772431
... alternatively, (edit: without needing a format change)...
Private Sub textAdjust_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Me.textAdjust.Value = Format(Me.textAdjust.Value, "$#,###,###.00")

'checks to see if adj amount is less than $150
If CDbl(Replace(Me.textAdjust.Value, "$", "")) > 150 Then
    MsgBox "Adjustment amount should be $150 or Less.  Please check the amount entered."
    Me.textAdjust.Value = ""
End If

End Sub

Open in new window

Regards,
Brian.
0
 

Author Closing Comment

by:"Abys" Wallace
ID: 38772688
Thank you... works perfectly without needing the format change
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38772694
Thanks, abys757.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now