Results 1 to 9 of 9

Thread: [RESOLVED] Excel - Identifying "valid" characters in two specific columns

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    12

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    12

    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 <> "'" ??

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    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?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    12

    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

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    12

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    12

    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

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    12

    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
  •  



Click Here to Expand Forum to Full Width