Consulting

Results 1 to 4 of 4

Thread: Solved: What is wrong with my Validation code?

  1. #1
    VBAX Regular
    Joined
    Jan 2008
    Posts
    75
    Location

    Wink Solved: What is wrong with my Validation code?

    Hi,

    I have jus written a code to validate the entries in column C in my worksheet which should be either Y or M. Below is the code I wrote, but I keep having error. Please, can someone help me out.


    Function MaintenanceFreqChecking(columnname As Integer)


    Dim rowcount
    Dim R
    Dim Y As String
    Dim M As String
    rowcount = Range("A65536").End(xlUp).Row
    For R = 2 To rowcount
    strVal = Sheet1.Cells(R, columnname).Value
    If strVal <> "Y" Or strVal <> "M" Then
    Sheet1.Cells(R, columnname).Interior.ColorIndex = 6
    End If


    Next


    End Function

    Thanks

    Lucpian


  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Change the Or to And:
    [VBA]
    If strVal <> "Y" And strVal <> "M" Then
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Or use conditional formatting.

    1 Select all the rows in column C you want to check.

    2 Goto Format>Conditional formatting.

    3 Select formula is from the dropdown.

    4 Enter this formula.

    =AND(C1<>"Y", C1<>"M")

    5 Format as required.

    PS You do realise Excel has Data>Validation...

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    the VBA code as modified, may still not give you exactly what you want because (1) it assumes that all cell colors are set to default (no fill?) when the procedure runs and (2) it does not check for the case where the cells contain leading or trailing spaces (if that is acceptable). To solve (1), you could clear all the colors from the relevant cells first. Another approach is to check for cell value = Y or M and if so explicitly set the cell color to default (no fill?); otherwise set it to the error color. I would do the latter because the code is more obvious:
    [vba]If strVal = "Y" or strVal = "M" then
    Sheet1.Cells(R, columnname).Interior.ColorIndex = xlNone
    Else
    Sheet1.Cells(R, columnname).Interior.ColorIndex = 6
    end if[/vba]
    If leading or trailing spaces or blanks around the "Y" or "M" are not acceptable, e.g., " Y" is not acceptable, nor is "M ", then the above is fine. If spaces are OK, then you could use the Trim functioin to trim away any leading or trailing blanks and the code might be:
    [vba]If Trim(strVal) = "Y" or strVal = "M" then
    Sheet1.Cells(R, columnname).Interior.ColorIndex = xlNone
    Else
    Sheet1.Cells(R, columnname).Interior.ColorIndex = 6
    end if[/vba]
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the 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
  •