• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Validating numeric values in Userform textbox|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Validating numeric values in Userform textbox|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosValidating numeric values in Userfo…
sp_PrintTopic sp_TopicIcon
Validating numeric values in Userform textbox
Avatar
Steve Leape

New Member
Members
Level 0
Forum Posts: 2
Member Since:
September 23, 2019
sp_UserOfflineSmall Offline
1
September 24, 2019 - 12:05 am
sp_Permalink sp_Print sp_EditHistory

Hi, - New here.

I'm baffled by an issue on a userform. I have two textbox's One called "Available1" and one called "Add1". "Available1" gets it's value from a cell on the sheet when the form fires up - Usually a number from 1 to a couple of hundred e.g. 250. I have the following code on the Change event of "Add1" to test for...

a) That a number is entered (i.e. Not text)

b) That the number is greater than 0

c) That the number is not higher than the value in "Available1"

d) That the number is a whole number (no decimal points)

 

Private Sub Add1_Change()

If Not IsNumeric(Me.Add1.Text) And Add1.Text <> "" Then

If Len(Add1.Text) > 1 Then

Add1.Text = Abs(Round(Left(Add1.Text, Len(Add1.Text) - 1), 0))

Else

Add1.Text = ""

End If

ElseIf Add1.Text <> "" Then

If Add1.Text = 0 Then

Add1.Text = ""

ElseIf Add1.Text > Available1.Text Then

If MsgBox "Are you sure you want to enter a number higher than the amount available?", vbYesNo + vbCritical) = vbNo Then

Add1.Text = ""

Else

Add1.Text = Abs(Round(Add1.Text, 0))

End If

End If

End Sub

All works fine apart from the greater than element. It seems to only recognise the first digit when applying the code. e.g. If "Available1" has a single digit number 1 - 8 if I entered 9 it would popup the message. However, if I entered 10 it would ignore the fact that its higher. It's as if it just recognises the first digit from each textbox. I have limited experience with VBA and am baffled. I'd be grateful for some assistance.

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1515
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
September 24, 2019 - 12:27 pm
sp_Permalink sp_Print

Hi Steve,

You're doing a string comparison with Add1.Text > Available1.Text rather than comparing numeric values.

Try using the Val() function to convert the strings to numbers.  If the value passed to Val is not a valid number, it returns 0

Val(Add1.Text) > Val(Available1.Text)

Read more here on Val and other string functions

Regards

Phil

Avatar
Steve Leape

New Member
Members
Level 0
Forum Posts: 2
Member Since:
September 23, 2019
sp_UserOfflineSmall Offline
3
September 25, 2019 - 6:29 pm
sp_Permalink sp_Print

Phil,

Many thanks. Your solution worked great.

Really grateful, thanks for taking the time to assist.

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1515
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
September 25, 2019 - 7:22 pm
sp_Permalink sp_Print

No worries Steve

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Chris Warren
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
michael serna
mashal sana
Tiffany Kang
Leah Gillmore
Sopi Yuniarti
LAFONSO HERNANDEZ
Hayden Hao
Angela chen
Sean Moore
John Chisholm
Forum Stats:
Groups: 3
Forums: 24
Topics: 6214
Posts: 27246

 

Member Stats:
Guest Posters: 49
Members: 31897
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.