VBA to validate cell input?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

Is it possible to validate user input into various cells? For example, Range C3:J3 is merged to create a cell where the user enters their name. Range C4:J4 is merged for them to enter their address. When they exit each range, I'd like to run some code to validate that they haven't entered anything like a comma or a speech mark (the data ultimately ends up in a database and I'm trying to keep it as clean as possible). If they have, I can just pop up a message box telling them what they need to correct.

Any thoughts appreciated.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I would recommend just using the built-in Data Validation. You can easily also set your own custom messages with Data Validation.
If you choose the Custom option, you can enter a formula like this for cell C3:
Excel Formula:
=COUNTIF(C3:C3,",")+COUNTIF(C3:C3,CHAR(34))=0

Note that CHAR(34) is the ASCII equivalent of ", which is what I assume you mean by "speech mark".
If you meant single-quote and not double-quote, you can use this instead:
Excel Formula:
=COUNTIF(C3:C3,",")+COUNTIF(C3:C3,",")=0

Also note, I HIGHLY recommend removing your merged cells. Merged cells are absolutely horrid and cause all sort of issues for Excel VBA.
It looks like you are just centering across rows, and the "Center Across Selection" formatting option gives you the same visual effect without all the issues that merged cells cause.
See here: Tom’s Tutorials For Excel: Using Center Across Selection Instead of Merging Cells – Tom Urtis
 
Upvote 0
Solution

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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