Vba validate if number, text or spaces

Joaonep

New Member
Joined
Jul 29, 2016
Messages
1
Hi guys i am not new to programming but for some time that i dont write nothing. At the moment i am trying to create a small code quit easy i would say and while testing in fake data works but with the real data doesnt.

I have a loop running through all the used cells looking for a specific string althrough i want to grab some info and keep going on each cicle i dont know how to validate if cell contains a double number only, if contains a string and a double number, if is empty or if has spaces......
Using instr i can find the string i need.

Using isempty i detect empty cells

Well with the irregular spaces is my problem so what can i do to detect or validate if that cell contains spaces only?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi and welcome to the Board!

Use VarType() function to determine type of value.
For example, type If VarType(Range("A1").Value) and then type equality symbol = to see the list of types.

For checking value with spaces only try this:
Rich (BB code):
  If Len(ActiveCell.Value) > 0 And Len(Trim(ActiveCell.Value)) = 0 Then
    MsgBox "Only spaces are in the active cell"
  End If

If values with spaces should be ignored as well as empty values then:
Rich (BB code):
Sub Test()
  Dim a(), v
  a() = Range("A1:A10").Value
  For Each v In a()
    If Len(Trim(v)) > 0 Then
      ' Nonempty and not only spaces
      Debug.Print v
    End If
  Next
End Sub
 
Last edited:
Upvote 0
...what can i do to detect or validate if that cell contains spaces only?
Try this...
Code:
If UBound(Split(ActiveCell.Value)) = Len(ActiveCell.Value) Then
  ' Cell contains one or more spaces and no other characters
End If
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,830
Members
449,190
Latest member
rscraig11

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