VBA data validation

Russk68

Well-known Member
Joined
May 1, 2006
Messages
589
Office Version
  1. 365
Platform
  1. MacOS
Hi All
I am using data validation for a list in column C. I also need to prevent a single space from being entered in column C, so I would like to use code in column C3:C5000 with a message "No space allowed"

Thank you in advance!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hey,

Highlight cells C3:C5000 and try a Custom data validation rule as such:

=COUNTIF($C$3:$C$5000,"* *")=0

Then you can write an error alert that says "No space allowed"
 
Upvote 0
Using Record Macro it comes out as this in VBA:

Code:
Sub ADv()
'
' ADv Macro
'


'
    Range("C3:C5000").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=COUNTIF($C$3:$C$5000,""* *"")=0"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = "No Space Allowed"
        .InputMessage = ""
        .ErrorMessage = "Please do not enter spaces in these cells."
        .ShowInput = True
        .ShowError = True
    End With
End Sub
 
Upvote 0
Hi Tyija
I pasted your code in the sheet where I want this applied. I can still type in a space or a word with a space. Any idea what I could be doing wrong?

Thank you for your help!
 
Upvote 0
Hi Tyija
I pasted your code in the sheet where I want this applied. I can still type in a space or a word with a space. Any idea what I could be doing wrong?

Thank you for your help!

Hi Russk, put the code in to a module rather than the sheet - then run the code and it should apply to your activesheet.

You can edit the code to target a specific sheet by adding the following code:
Code:
Sheets("Your sheet name goes here").Activate

Place that BEFORE the Range("C3:C5000").Select

EDIT: This should work better than previous code:

Code:
Sub ADv()
'
' ADv Macro
'




'
    Sheets("Your sheet name goes here").Activate
    Range("C3:C5000").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=COUNTIF(C3,""* *"")=0"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = "No Space Allowed"
        .InputMessage = ""
        .ErrorMessage = "Please do not enter spaces in these cells."
        .ShowInput = True
        .ShowError = True
    End With
End Sub
 
Last edited:
Upvote 0
Thank Tyija
That works but...
The validation list that I was using in that column is gone.
I always wondered if it is possible to have more than one validation rule. Is vba the only way to do it?
Would I need to add code to get the list working again?
 
Upvote 0
You can combine rules to a certain extent, depending on the criteria, but technically you can only have 1 data validation rule active for a certain cell.

In this case the macro will make your desired tab in the range C3:C5000 - not allow any spaces. If you had a previous rule before in the same range, the code does overwrite it - what exactly was the previous rule?
 
Last edited:
Upvote 0
Try this:
Put the code in the sheet's code module.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C3:C5000")) Is Nothing Then
        If Target.Cells.Count = 1 Then
            If InStr(Target.Value, " ") Then
            Target.Activate
            MsgBox "No space allowed"
            End If
        End If
    End If
End Sub
 
Upvote 0
That helps alot! I'm still allowed to type a space but I added a couple of lines of code to replace the formula which I was trying to protect.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,744
Messages
6,174,254
Members
452,553
Latest member
red83

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