Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88054

Looping through visible rows only not working

$
0
0

I am trying to write a piece of code, that will color all requests which contain only one unique name for each request. Why looping trough visible cells only is not working?

UPDATE: I need to delete rows if there is only one name assigned to a particular request

So for below request I would like to remove Mary H (since her name appears only once in the request)

Request Number  Client Contact Assignee: Full Name
4350257 Eleanor B
4350257 Eleanor B
4350257 Mary H

This request is ok, no need to delete anything

8620428 Kevin M
8620428 Kevin M

In this request I would like to remove Mary H and Julia K since there names appears only once in the request)

7208497 Michael W
7208497 Mary H
7208497 Michael W
7208497 Julia K

My CODE:

Sub Testing()

Sheet1.Select

Dim r As Long, LR As Long
Dim ReqNo As Long, CCFullName As Long
Dim rgn2 As Range

LR = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

'Request Number
ReqNo = Application.Match("Request Number", Sheet1.Rows(1), 0)
'Client Contact Assignee: Full Name
CCFullName = Application.Match("Client Contact Assignee: Full Name", Sheet1.Rows(1), 0)

Set rgn2 = Columns(CCFullName)

Dim cl As Range, rng As Range, x As Long

Set rng = Range("A2:A100")
Dim cell As Range

With Range("A2:A100").SpecialCells(xlCellTypeVisible)
   For x = .Rows.Count To 1 Step -1
       Set cell = Range("A"& x) ' this sets the current cell in the loop
            For Each cl In rng.SpecialCells(xlCellTypeVisible)
                For r = LR To 2 Step -1
                    If Application.WorksheetFunction.CountIf(rgn2, Cells(r, CCFullName).Value) = 1 Then
                        Rows(r).Interior.Color = rgbBlueViolet
                    End If
                Next r
        Next cl
    Next x
End With
End Sub

The code above only colors names that are unique for the whole document, which is Mary H, Anna W and Thomas Y. However, I need the code to include also the 3 below names which occur once only in a particular request. (This is just a sample)

7208497 Kevin M
7208497 Julia K
8138382 Shahida B

Sample data:

Request Number  Client Contact Assignee: Full Name
4350257 Eleanor B
4350257 Eleanor B
4350257 Mary H
8620428 Kevin M
8620428 Kevin M
7208497 Michael W
7208497 Kevin M
7208497 Michael W
7208497 Julia K
7191212 Thomas Y
7191212 Shahida B
7191212 Shahida B
7191212 Shahida B
8138382 Julia K
8138382 Julia K
8138382 Shahida B
8138382 Julia K
8138382 Anna W

Viewing all articles
Browse latest Browse all 88054

Trending Articles