User form field should contain numbers only

Chewyhairball

Active Member
Joined
Nov 30, 2017
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi

I have this bit of code that Textbox2(called row2 on my user form) that displays a message box if no data has been entered into the textbox, and it works fine.
VBA Code:
If Trim(Me.Row2.Value) = "" Then
  Me.Row2.SetFocus
  MsgBox "Please enter a Value"
  Exit Sub
End If

I have been trying to create a similar things but to display a message box if the data entered into the textbox is 'text or space' and not a number.
It doesnt work and triggers the message box whether i enter a number or text or a space. :(
Any help would be much appreciated.
VBA Code:
If Application.WorksheetFunction.IsText(Trim(Me.Row2.Value)) Then
  Me.Row2.SetFocus
  MsgBox "This field should contain numbers only!" & vbNewLine & vbNewLine & "Please check that no text or a 'space' has been entered into this field", 64, "RECOVERY FIELD ERROR!"
  Exit Sub
End If
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about like this?

VBA Code:
If Not IsNumeric(Trim(Me.Row2.Value)) Then
  Me.Row2.SetFocus
  MsgBox "This field should contain numbers only!" & vbNewLine & vbNewLine & "Please check that no text or a 'space' has been entered into this field", 64, "RECOVERY FIELD ERROR!"
  Exit Sub
End If
 
Upvote 0
Solution
How about like this?

VBA Code:
If Not IsNumeric(Trim(Me.Row2.Value)) Then
  Me.Row2.SetFocus
  MsgBox "This field should contain numbers only!" & vbNewLine & vbNewLine & "Please check that no text or a 'space' has been entered into this field", 64, "RECOVERY FIELD ERROR!"
  Exit Sub
End If
Hi

Works great! thanks very much for your help :)
 
Upvote 0

Forum statistics

Threads
1,223,317
Messages
6,171,425
Members
452,402
Latest member
siduslevis

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