Consulting

Results 1 to 5 of 5

Thread: Select a couple of cells per row

  1. #1
    VBAX Regular
    Joined
    Jun 2015
    Posts
    21
    Location

    Select a couple of cells per row

    Hello,

    I want to check whether a name exist more than ones in one row.
    Then a want to check the next row. And so one.

    I can use Range("D2, F2, H2, J2, L2"). But that is not flexible. I want to use a for next loop.

    In the file you will find my code.

    Do one of you know how I can fix this problem?

    Kind regards,
    Willem
    Attached Files Attached Files

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello wmr,

    This version of the macro should be easier to understand and modify. It will add the Duplicates Conditional Formatting to column D, F, H, and J starting with row 2 down to the last row in column "A".

    Sub AddCondFormat()
    
        Dim i       As Long
        Dim LastRow As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Worksheets("Duplicate Names")
            Set Rng = Range("D2, F2, H2, J2, L2")
    
            LastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
            If LastRow < Rng.Row Then Exit Sub
            
            For i = 0 To LastRow - Rng.Row
                For Each Cell In Rng.Offset(i, 0)
                    With Cell
                        .FormatConditions.Delete
                        With .FormatConditions
                            .Add xlUniqueValues
                            .Item(1).ModifyAppliesToRange Rng.Offset(i, 0)
                            .Item(1).DupeUnique = xlDuplicate
                            .Item(1).Font.Color = -16383844
                            .Item(1).Font.TintAndShade = 0
                            .Item(1).Interior.PatternColorIndex = xlAutomatic
                            .Item(1).Interior.Color = 13551615
                            .Item(1).Interior.TintAndShade = 0
                            .Item(1).StopIfTrue = False
                        End With
                    End With
                Next Cell
            Next i
    
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    VBAX Regular
    Joined
    Jun 2015
    Posts
    21
    Location
    Hello Leith,

    Thank you for your answer.

    Your macro only works for John. In the second line their is twice the name of Susan.

    I am looking for a solution so that I can use i in a for next loop.

    So
    "D" & i
    "F" & i
    "H" & i
    "J" & i
    "L" & i

    Kind regards
    Willem

  4. #4
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello wmr,

    You probably meant to enter "Susan" twice. In your example workbook, you entered "Susan" and "Suzan" which are two different names.

    The way the loop is written it does step through the cells rows by using "i". The loop starts at zero instead of one because "i" is now the row offset to our starting Range of cells "D2", "F2", "H2", "J2", and "L2". The second loop "For Each Cell", applies the same conditional formatting to each cell in the Range("D2", "F2", "H2", "J2", "L2").Offset(i, 0). It is important to remember that "i" is a relative row reference within this range itself and not an absolute reference to the row on the worksheet. The method is a bit different than what you wanted to do but accomplishes the same goal.

    You can do it your way like this...
    Sub Macro1()
    
        Dim i       As Long
        Dim LastRow As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Worksheets("Duplicate Names")
    
            LastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
            If LastRow < 2 Then Exit Sub
    
            For i = 2 To LastRow
                Call AddCF(Wks.Range("D" & i))
                Call AddCF(Wks.Range("F" & i))
                Call AddCF(Wks.Range("H" & i))
                Call AddCF(Wks.Range("J" & i))
                Call AddCF(Wks.Range("L" & i))
            Next i
            
    End Sub
    
    Sub AddCF(ByRef Cell As Range)
    
            With Cell
                .FormatConditions.Delete
                With .FormatConditions
                    .Add xlUniqueValues
                    .Item(1).ModifyAppliesToRange Rng.Offset(i, 0)
                    .Item(1).DupeUnique = xlDuplicate
                    .Item(1).Font.Color = -16383844
                    .Item(1).Font.TintAndShade = 0
                    .Item(1).Interior.PatternColorIndex = xlAutomatic
                    .Item(1).Interior.Color = 13551615
                    .Item(1).Interior.TintAndShade = 0
                    .Item(1).StopIfTrue = False
                End With
            End With
       
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  5. #5
    VBAX Regular
    Joined
    Jun 2015
    Posts
    21
    Location
    Hello Leith,

    Thanks again!

    Excecuting this line: .Item(1).ModifyAppliesToRange Rng.Offset(i, 0) the message "Object required" appears.

    I do not understand what the problem is.

    Kind regards,
    Willem

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
  •