-
Aug 16th, 2012, 11:50 AM
#1
Thread Starter
New Member
[RESOLVED] Excel - Identifying "valid" characters in two specific columns
Good afternoon everyone -
Here's my situation, I have two columns of information that I want to "validate" before the info in those cells is used in a series of macros.
The columns in question are AF and AG, and run from rows 3 through Range("M65536").End(xlUp).Row
The "valid" characters are numbers 1 through 0 and the apostrophe (').
If the cell contains "invalid" characters I want it to shade the cell red, change font to white, and add 1 to a an integer that will feed a message box that states it found "x" number of invalid cells.
If the cells all contain nothing but valid characters then the macro continues with no further action required.
My thanks to anyone that can help,
Frank M.
-
Aug 16th, 2012, 12:36 PM
#2
Re: Excel - Identifying "valid" characters in two specific columns
Try something like this (but it doesn't handle a single ' character):
Code:
Sub validate()
Dim lastRow As Long
Dim i As Integer
Dim testVal1 As String
Dim testVal2 As String
Dim counter As Long
lastRow = Range("af" & Rows.Count).End(xlUp).Row
For i = 3 To lastRow
testVal1 = Range("af" & i).Value
If testVal1 <> "0" And testVal1 <> "1" And testVal1 <> "'" Then
Range("af" & i).Interior.Color = vbRed
Range("af" & i).Font.Color = vbWhite
counter = counter + 1
End If
testVal2 = Range("ag" & i).Text
If testVal2 <> "0" And testVal2 <> "1" And testVal2 <> "'" Then
Range("ag" & i).Interior.Color = vbRed
Range("ag" & i).Font.Color = vbWhite
counter = counter + 1
End If
Next i
End Sub
-
Aug 17th, 2012, 08:00 AM
#3
Thread Starter
New Member
Re: Excel - Identifying "valid" characters in two specific columns
Thanks vbfbryce, I'll give that a try. One question tho'...and I think I know the answer...do I have to make a seperate entry for each of the numbers? Ie., If testVal1 <> "0" And testVal1 <> "1" And testVal1 <> "2" And testVal1 <> "3" And testVal1 <> "4" And testVal1 <> "5" And testVal1 <> "6" And testVal1 <> "7" And testVal1 <> "8" And testVal1 <> "9" And testVal1 <> "'" ??
-
Aug 17th, 2012, 08:35 AM
#4
Re: Excel - Identifying "valid" characters in two specific columns
Sorry, I misunderstood. I thought you were only trying to look for "0" or "1" or "'" rather than 1,2,3,4,5...
This:
Code:
If Not IsNumeric(testVal1) And Range("af" & i).PrefixCharacter <> "'" Then
will check for either a numeric value or an apostrophe.
Will that do what you're looking for?
-
Aug 17th, 2012, 01:45 PM
#5
Thread Starter
New Member
Re: Excel - Identifying "valid" characters in two specific columns
I entered the code as above (including the "Not IsNumeric" modification) but it didn't catch an error that I purposefully enetered to test the code. I entered ";0615" in AF3 and the code passed right over it. All of my data will be four digits (times) with the possibility of an apostrophe in front of any times that start with a zero(s) in order to keep the full four digits (ie., '0030, '0615, 1230, etc.)
One concern - when you set testVal1 you used .Value, but when you set testVal2 you used .Text - was that a typo? Will it make a difference?
Thanks for your help,
Frank
-
Aug 18th, 2012, 04:17 AM
#6
Re: Excel - Identifying "valid" characters in two specific columns
when you set testVal1 you used .Value, but when you set testVal2 you used .Text - was that a typo? Will it make a difference?
.value is the content of the cell, .text is the displayed content, which includes cell formatting
another method to test if the cell content is numeric
Code:
if val(cell.value = cell.value then
this will only work with whole numbers or decimal character of .
All of my data will be four digits (times) with the possibility of an apostrophe in front of any times that start with a zero(s) in order to keep the full four digits (ie., '0030, '0615, 1230, etc.)
values preceded with ' are considered, by excel, to be strings, rather than numeric, so those with ' will by default justify to left of the cell, whereas numeric values will justify to right, though value will consider them to be the same, but not =
Last edited by westconn1; Aug 18th, 2012 at 04:25 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 21st, 2012, 10:34 AM
#7
Thread Starter
New Member
Re: Excel - Identifying "valid" characters in two specific columns
What about assigning my valid characters to an array and comparing the contents of each cell to the array? Or would I have to put in each possible times combo, ie., 0015, 0030, 0045, 0100, 0115, etc.?
Or maybe examining each character individually using Left, Mid, and/or Right?
Thanks,
Frank
-
Aug 21st, 2012, 12:34 PM
#8
Thread Starter
New Member
Re: Excel - Identifying "valid" characters in two specific columns
I used the following...
Ext = Mid$(ActiveCell, f, 1)
If InStr(1, "'0123456789", Ext, vbTextCompare) <= 0 Then
-
Aug 21st, 2012, 12:51 PM
#9
Thread Starter
New Member
Re: Excel - Identifying "valid" characters in two specific columns
Oops...not sure how that posted on me...should have continued lke this...
in place of...
Code:
If Not IsNumeric(testVal1) And Range("af" & i).PrefixCharacter <> "'" Then
And it seems to work great...
thanks,
Frank
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|