• 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
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Test existence of any number in input string|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Test existence of any number in input string|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
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 & MacrosTest existence of any number in inp…
sp_PrintTopic sp_TopicIcon
Test existence of any number in input string
Avatar
Kevin Junk
Member
Members
Level 0
Forum Posts: 23
Member Since:
October 18, 2018
sp_UserOfflineSmall Offline
1
August 16, 2019 - 4:09 pm
sp_Permalink sp_Print

I need to test the string that will be entered into an InputBox to detect if there are any numbers in the string, eg. AB4C, 9hgTRe, CvFgr54Xt etc  - these need to be caught. The string can be of any length.

I have a feeling that this may involve Regex but I hope not 🙂

Thanks!

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
August 16, 2019 - 10:55 pm
sp_Permalink sp_Print sp_EditHistory

You can simply use the Like operator:

If "aaz1" like "*#*" then 'it has digits

But best is to restrict what users are allowed to type.

Application.InputBox is different than just InputBox, see this article. The first calls the InputBox method, the second calls the InputBox function.

You can restrict values to text only, or to numbers, ranges, or combinations.

For complex restrictions, you can take a look at this article, with user forms, this allows you to force users to type values in a specific range.

Avatar
Kevin Junk
Member
Members
Level 0
Forum Posts: 23
Member Since:
October 18, 2018
sp_UserOfflineSmall Offline
3
August 17, 2019 - 2:47 pm
sp_Permalink sp_Print

Thanks mate, I didn't know there were two types of InputBox ... <sigh>

Anyway I tested it and it didn't work in the sense that it allowed me to enter a number when I suppose it shouldn't have.

Here is my code:

Dim TestString As Variant

TestString = Application.InputBox(prompt:="Please enter your text", Title:="Export File Data", Type:=2)

MsgBox "You have entered: " & TestString

This didn't object to me entering a number. What should have happened? What am I doing wrong?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
August 17, 2019 - 5:28 pm
sp_Permalink sp_Print sp_EditHistory

The input box will return a text string, even if you type only numbers in InputBox, even if Type:=2.

you can test the type of the result with : MsgBox TypeName(TestString)

Type:=2 will return String, no matter what chars you type, if Type:=1 will return Double.

You wil have to check the result with: If "aaz1" like "*#*", or use the link I sent to use a userform instead to restrict the input to alpha only.

Avatar
Kevin Junk
Member
Members
Level 0
Forum Posts: 23
Member Since:
October 18, 2018
sp_UserOfflineSmall Offline
5
August 18, 2019 - 8:20 am
sp_Permalink sp_Print

OK so that's a fail - I'm not sure about the purpose of Application.InputBox or why its different to the InputBox function after all. According to MS the Type parameter "Specifies the return data type. If this argument is omitted, the dialog box returns text.", but it seems it always returns text no matter what. I don't see how this can enable you to "... restrict values to text only, or to numbers, ranges, or combinations."

Anyway, I'll have to come up with something else ...

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1529
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
August 18, 2019 - 1:06 pm
sp_Permalink sp_Print

Hi Kevin,

I'm not sure what isn't working for you.  Doing as Catalin suggests works fine for me e.g.

  If TestString Like "*#*" Then Debug.Print "Numbers"

will detect if a number has been typed in.  Once you detect this you then have to prompt for input again.

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Mynda Treacy
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 215
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Anthony van Riessen
Erlinda Eloriaga
Abisola Ogundele
MARTYN STERRY
Rahim Lakhani
Ngoc Qui Nguyen
Clement Mansfield
Rose .
Bindu Menon
Baruch Zemer
Forum Stats:
Groups: 3
Forums: 24
Topics: 6352
Posts: 27779

 

Member Stats:
Guest Posters: 49
Members: 32319
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.