VBA to check number of digits in cell

boliver228

New Member
Joined
Feb 15, 2013
Messages
15
Hi,

I have a macro which submits a spreadsheet to an excel site. The macro checks that a number of compulsory fields are populated before it allow this spreadsheet to be submitted, and if any checks fail, it displays a message box and exits the macro.

One of the checks is to make sure a cell [CRM_Reference] contains either a 6 or 9 digit integer. I've tried the code below but it doesn't work

Dim Chars1 as Integer
Dim Chars2 as Integer
Chars1= 9
Chars2 = 9
If Len(Range("CRM_Reference").Text)) <> Chars 1 Or Len(Range("CRM_Reference").Text)) <> Chars 2 Then
MsgBox "Insert Message Here"
Exit Sub
End If

Any ideas why not?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Perhaps
Code:
If cellText Like "######" Or cellText Like "#########" Then 
    Msgbox "good integer"
End If
 
Upvote 0
Perhaps
Code:
If cellText Like "######" Or cellText Like "#########" Then 
    Msgbox "good integer"
End If

Thanks but this only works for the 6 digits. Code below:

If Not Range("CRM_Reference").Text Like "######" Or Range("CRM_Reference").Text Like "#########" Then

Any ideas?
 
Upvote 0
Hi

You forgot the parentheses:

Code:
If Not (Range("CRM_Reference").Text Like "######" Or Range("CRM_Reference").Text Like "#########") Then
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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