Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
cdb cdb is offline
external usenet poster
 
Posts: 62
Default Data validation of cells using code

I am currently working on a spreadsheet and several cells I need to have
validation on to only allow entry of either numbers or text only dependant on
which cell it is.

For example, in cell C5 I would like it to only allow text characters (no
numbers) and in C6 I would like it to only accept numbers or spaces (for a
phone number).

I have messed around with various attempts at using just the validation
worksheet functions, but it doesn't fill my requirements (ie for text, it
will still accept numbers so long as there is some text in it somewhere and
for the phone numbers I can't include spaces).

Is there any way I can put some code in to automatically check the entries
of these cells and validate as required?

Hope this all makes sense.

cdb
  #2   Report Post  
Posted to microsoft.public.excel.programming
cdb cdb is offline
external usenet poster
 
Posts: 62
Default Data validation of cells using code

This is the code I've managed to work out so far, but for some reason it
loops indefinitely. Can anyone help??

Private Sub worksheet_change(ByVal Target As Range)

If Target.Address(False, False) = "C5" Then
Target.Value = UCase(Target.Value)
For X = 0 To 255 'looks at every ascii character
If (X = 65 And X <= 90) Then
'if is between A-Z or 0-9 then do nothing
Else
Target.Value = Replace(Target.Value, Chr(X), "")
'otherwise replace it with nothing
End If
Next X 'so starts at 0 then goes to 1, then 2....
Else
End If
End Sub

TIA,

cdb

"cdb" wrote:

I am currently working on a spreadsheet and several cells I need to have
validation on to only allow entry of either numbers or text only dependant on
which cell it is.

For example, in cell C5 I would like it to only allow text characters (no
numbers) and in C6 I would like it to only accept numbers or spaces (for a
phone number).

I have messed around with various attempts at using just the validation
worksheet functions, but it doesn't fill my requirements (ie for text, it
will still accept numbers so long as there is some text in it somewhere and
for the phone numbers I can't include spaces).

Is there any way I can put some code in to automatically check the entries
of these cells and validate as required?

Hope this all makes sense.

cdb

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Data validation of cells using code

Afternoon CDB,

I wasn't able to come up with a code that could work however, if you were to
create some type of validation yourself using the condition formatting, that
could work.

Eg. input cell "D1", 2 condition cell "E1" and "E2", E1 and F2 have formulas
"=if($D$10,$D$1,0). Use Cell E2 with formulas (=sum(E1+E2)). Using condition
formatting, state that (=E20) then paint cell red.

Try and see if it works.

Cheers,


"cdb" wrote:

This is the code I've managed to work out so far, but for some reason it
loops indefinitely. Can anyone help??

Private Sub worksheet_change(ByVal Target As Range)

If Target.Address(False, False) = "C5" Then
Target.Value = UCase(Target.Value)
For X = 0 To 255 'looks at every ascii character
If (X = 65 And X <= 90) Then
'if is between A-Z or 0-9 then do nothing
Else
Target.Value = Replace(Target.Value, Chr(X), "")
'otherwise replace it with nothing
End If
Next X 'so starts at 0 then goes to 1, then 2....
Else
End If
End Sub

TIA,

cdb

"cdb" wrote:

I am currently working on a spreadsheet and several cells I need to have
validation on to only allow entry of either numbers or text only dependant on
which cell it is.

For example, in cell C5 I would like it to only allow text characters (no
numbers) and in C6 I would like it to only accept numbers or spaces (for a
phone number).

I have messed around with various attempts at using just the validation
worksheet functions, but it doesn't fill my requirements (ie for text, it
will still accept numbers so long as there is some text in it somewhere and
for the phone numbers I can't include spaces).

Is there any way I can put some code in to automatically check the entries
of these cells and validate as required?

Hope this all makes sense.

cdb

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom data validation on cells with data validation values AKrobbins Excel Worksheet Functions 2 June 21st 11 04:20 PM
Data Validation in code... Theresa Smallwood Excel Programming 3 August 1st 05 09:17 PM
VB code + Data Validation query Jennyc114 Excel Programming 6 July 5th 05 11:28 AM
Data validation code Gareth[_3_] Excel Programming 0 November 26th 03 06:01 PM
Data validation using code Gareth[_3_] Excel Programming 1 November 26th 03 12:27 AM


All times are GMT +1. The time now is 01:25 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"