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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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,215,772
Messages
6,126,811
Members
449,339
Latest member
Cap N

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