Consulting

Results 1 to 13 of 13

Thread: Validate cells in column A with info from other columns

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    17
    Location

    Red face Validate cells in column A with info from other columns

    Hi,

    I am trying to validate with OK/CHECK values from column A. The result goes into column B for each value in Row A.
    The values I am trying to do the validation come from 2 different columns ( G and H).

    For Example:

    Column A has a variety of Last Name + First Name combinations written in all possible forms (John Deer; Deer, John; Deer John; Acapulco hotel Deer John)
    Columns G and H are first Name Last Name of the people that are valid and which i need to do the validation for. needles to say these 2 need to be from the same row.

    The code i wrote so far is the one below but unfortunately the result is CHECK from top to bottom. I have a feeling i am doing something very wrong with the info coming from G and H.

    Sub Match()
    
    Dim cell As Range
    
    
     For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
    
        If InStr(Cells(cell.Row, "A"), Cells(cell.Column, "H")) > 0 And _
        InStr(Cells(cell.Row, "A"), Cells(cell.Column, "I")) > 0 Then
              
           
         Cells(cell.Row, "B") = "OK"
         
         Else
         
         Cells(cell.Row, "B") = "Check"
         
         
    End If
    Next cell
    
    
    End Sub

    Let me know if you need a sample of the file.

    Kind regards,
    Shywawa

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    post your workbook pls.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,190
    Location
    Hi there

    I am on my phone so can’t test code but looking at this I would ask:

    Does the function Instr require values of a cell or a string to look at?

    The code above is passing the cell itself to Instr and may cause issues.

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    VBAX Regular
    Joined
    Jan 2018
    Posts
    17
    Location
    Hi mancubus, attached.

    Hi georgiboy, a string in a cell. I just want to make this validation and this was the idea I had before getting stuck.

    CHECK Names.xlsm

    Thank you!
    Shywawa

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Updated after attachment posted


    Assuming I understood ....

    Capture.JPG

    Option Explicit
     
    Sub Match()
        Dim rCell As Range
         
        With ActiveSheet
            For Each rCell In Range(.Cells(2, 1), .Cells(2, 1).End(xlDown)).Cells
                 'not case sensitive
                If InStr(1, rCell.Value, .Cells(rCell.Row, 7), vbTextCompare) > 0 And InStr(1, rCell.Value, .Cells(rCell.Row, 8), vbTextCompare) > 0 Then
                    .Cells(rCell.Row, 2) = "OK"
                Else
                    .Cells(rCell.Row, 2) = "Check"
                End If
           Next
        End With
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 01-31-2018 at 07:47 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular
    Joined
    Jan 2018
    Posts
    17
    Location
    Hi Paul,

    My code was producing similar hits if the names were on the same row. Unfortunately if you sort AtoZ or ZtoA in G or H the macro won't work anymore.
    Sadly the file i am using has a lot more names in column G and H and it needs to work independently of the row where the values are. Attached a screenshot from the file you posted with a simple sort.

    Capture2.jpg

    Please advise.


    Kind regards,
    DD

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Column A has a variety of Last Name + First Name combinations written in all possible forms (John Deer; Deer, John; Deer John; Acapulco hotel Deer John)
    Columns G and H are first Name Last Name of the people that are valid and which i need to do the validation for. needles to say these 2 need to be from the same row.
    I misunderstood 'same row'

    Anyways, it's easy enough

    Option Explicit
     
    Sub Match()
        Dim rCell As Range
        Dim aNames As Variant
        Dim r As Long
         
        With ActiveSheet
        
            aNames = .Cells(1, 7).CurrentRegion.Value
        
            For Each rCell In Range(.Cells(2, 1), .Cells(2, 1).End(xlDown)).Cells
                .Cells(rCell.Row, 2) = "Check"
                 
                 For r = LBound(aNames, 1) + 1 To UBound(aNames, 1)
                     'not case sensitive
                    If InStr(1, rCell.Value, aNames(r, 1), vbTextCompare) > 0 And InStr(1, rCell.Value, aNames(r, 2), vbTextCompare) > 0 Then
                        .Cells(rCell.Row, 2) = "OK"
                        Exit For
                    End If
                Next r
           Next
        End With
    End Sub

    However, since InStr is used, there is chance for error. A better solution will be more complicated


    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,190
    Location
    There is probably a much simpler way of doing this but thought i would post my best attempt:

    Function removeSpecial(sInput As String) As String    Dim sSpecialChars As String
        Dim i As Long
        sSpecialChars = ",;"
        For i = 1 To Len(sSpecialChars)
            sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
        Next
        removeSpecial = sInput
    End Function
    
    
    Sub SearchNames()
        Dim FstLstRng As Range, rCell As Range, CheckCells As Range
        Dim NameList As Variant, Nme As Variant
        Dim Str As String, a As Long, b As Long, c As Long
    
        Set FstLstRng = Range("G2:H" & Range("G" & Rows.Count).End(xlUp).Row).Cells
        Set CheckCells = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Cells
        
        NameList = FstLstRng
        
        For Each rCell In CheckCells.Cells
            rCell.Offset(, 1).Value = "CHECK"
            Str = removeSpecial(rCell.Value)
            Nme = Split(Str, " ")
            For a = 0 To UBound(Nme)
                For b = 1 To UBound(NameList)
                    If UCase(Nme(a)) = UCase(NameList(b, 1)) Then
                        For c = 0 To UBound(Nme)
                            If UCase(Nme(c)) = UCase(NameList(b, 2)) Then
                                rCell.Offset(, 1).Value = "OK"
                                Exit For
                            End If
                        Next c
                    End If
                Next b
            Next a
            For a = 0 To UBound(Nme)
                For b = 1 To UBound(NameList)
                    If UCase(Nme(a)) = UCase(NameList(b, 2)) Then
                        For c = 0 To UBound(Nme)
                            If UCase(Nme(c)) = UCase(NameList(b, 1)) Then
                                rCell.Offset(, 1).Value = "OK"
                                Exit For
                            End If
                        Next c
                    End If
                Next b
            Next a
        Next rCell
    End Sub
    May need to add some more special characters

    Hope this helps
    Last edited by georgiboy; 01-31-2018 at 04:12 PM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  9. #9
    VBAX Regular
    Joined
    Jan 2018
    Posts
    17
    Location
    Hi Paul,

    Thank you for the code. This will work, I am aware it might backfire on a few names but it should be ok. My next step will be to add the names that came from the OK check in the next column.
    If you have any idea let me know. I will mark this thread as solved later on today.


    Hi Georgiboy,

    I ran your code also but it only gave Check from top to bottom.


    Thank you both very much for the help on this one.


    Kind regards,
    Shywawa

  10. #10
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,190
    Location
    How strange, here is the file i used to test:

    CompName.xlsm
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  11. #11
    VBAX Regular
    Joined
    Jan 2018
    Posts
    17
    Location
    Quote Originally Posted by georgiboy View Post
    How strange, here is the file i used to test:

    CompName.xlsm
    This is working!

    Thank you for the code! If you have any idea on how to put the matching names next to the validation (next column) please shoot!
    I will crunch my brains on it today and if I can't find anything will open a new thread. Or should I keep this one open and update the name?

  12. #12
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,190
    Location
    Maybe:

    Function removeSpecial(sInput As String) As String    
        Dim sSpecialChars As String
        Dim i As Long
        sSpecialChars = ",;"
        For i = 1 To Len(sSpecialChars)
            sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
        Next
        removeSpecial = sInput
    End Function
     
    Sub SearchNames()
        Dim FstLstRng As Range, rCell As Range, CheckCells As Range
        Dim NameList As Variant, Nme As Variant
        Dim Str As String, a As Long, b As Long, c As Long
         
        Set FstLstRng = Range("G2:H" & Range("G" & Rows.Count).End(xlUp).Row).Cells
        Set CheckCells = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Cells
        
        NameList = FstLstRng
         
        For Each rCell In CheckCells.Cells
            rCell.Offset(, 1).Value = "CHECK"
            Str = removeSpecial(rCell.Value)
            Nme = Split(Str, " ")
            For a = 0 To UBound(Nme)
                For b = 1 To UBound(NameList)
                    If UCase(Nme(a)) = UCase(NameList(b, 1)) Then
                        For c = 0 To UBound(Nme)
                            If UCase(Nme(c)) = UCase(NameList(b, 2)) Then
                                rCell.Offset(, 1).Value = NameList(b, 1)
                                rCell.Offset(, 2).Value = NameList(b, 2)
                                Exit For
                            End If
                        Next c
                    End If
                Next b
            Next a
            For a = 0 To UBound(Nme)
                For b = 1 To UBound(NameList)
                    If UCase(Nme(a)) = UCase(NameList(b, 2)) Then
                        For c = 0 To UBound(Nme)
                            If UCase(Nme(c)) = UCase(NameList(b, 1)) Then
                                rCell.Offset(, 1).Value = NameList(b, 1)
                                rCell.Offset(, 2).Value = NameList(b, 2)
                                Exit For
                            End If
                        Next c
                    End If
                Next b
            Next a
        Next rCell
    End Sub
    Last edited by georgiboy; 02-01-2018 at 06:05 AM.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  13. #13
    VBAX Regular
    Joined
    Jan 2018
    Posts
    17
    Location
    Perfect!



    Thank you all for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •