[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.
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
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 <> "'" ??
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?
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
Re: Excel - Identifying "valid" characters in two specific columns
Quote:
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 .
Quote:
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 =
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
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
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