Excel VBA LookUp

R

robin.coe

I have the following piece of code which removes duplicate rows based
off column C.

LastRow = Range("C65536").End(xlUp).Row
For x = LastRow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range("C10:C" & x),
Range("C" & x).Text) > 1 Then
Range("C" & x).EntireRow.Delete
End If
Next x

I want to expand it to also look at what is in column E BEFORE
deleting the rows to see if column E has the following text "ZZ" . If
column E has that text THEN delete the entire rows.

Can someone help with this ??
 
D

Dave Peterson

Maybe...

Option Explicit
Sub testme()

Dim LastRow As Long
Dim iRow As Long

With ActiveSheet
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
For iRow = LastRow To 1 Step -1
If Application.CountIf(.Range("C10:C" & iRow), _
.Cells(iRow, "C").Text) > 1 Then
'check for XX
If LCase(.Cells(iRow, "E").Value) = LCase("ZZ") Then
'ok to delete
.Rows(iRow).Delete
End If
End If
Next iRow
End With

End Sub
 
R

robin.coe

Maybe...

Option Explicit
Sub testme()

     Dim LastRow As Long
     Dim iRow As Long

     With ActiveSheet
         LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
         For iRow = LastRow To 1 Step -1
             If Application.CountIf(.Range("C10:C" & iRow),_
                                 .Cells(iRow, "C").Text) > 1 Then
                 'check for XX
                 If LCase(.Cells(iRow, "E").Value) = LCase("ZZ") Then
                     'ok to delete
                     .Rows(iRow).Delete
                 End If
             End If
         Next iRow
     End With

End Sub

Thank you, this works by deleting the row with the text "ZZ" on it.
But I'm checking column C for duplicates first and then column E for
the text "ZZ"....I want it to delete both the rows that have the
duplicates since it found the text "ZZ" in one of the rows.
Suggestions ??
 
D

Don Guillett Excel MVP

Thank you, this works by deleting the row with the text "ZZ" on it.
But I'm checking column C for duplicates first and then column E for
the text "ZZ"....I want it to delete both the rows that have the
duplicates since it found the text "ZZ" in one of the rows.
Suggestions ??- Hide quoted text -

- Show quoted text -

"If desired, send your file to dguillett @gmail.com I will only look
if:
1. You send a copy of this message on an inserted sheet
2. You give me the newsgroup and the subject line
3. You send a clear explanation of what you want
4. You send before/after examples and expected results."
 
D

Dave Peterson

I don't understand what you want.

But this may help you. Create a new helper column that contains a formula that
returns what you want.

Maybe something like:
=c1&"|"&e1
(and drag down)

Then you can just check that single column in your original code.
 
R

robin.coe

I don't understand what you want.

But this may help you.  Create a new helper column that contains a formula that
returns what you want.

Maybe something like:
=c1&"|"&e1
(and drag down)

Then you can just check that single column in your original code.

Adding the data from the different columns into one column doesn't
help.
Let's say cell C1 has "ABC" in it and E1 has "AA"
C2 has "DEF" in it and E2 has "BB"
C3 has "DEF" in it and E3 has "ZZ"
C4 has "GHI" in it and E4 has "CC"
C5 has "GHI" in it and E5 has "DD"
I want the code to recognize that C2 & C3 have the same data in them
AND because E3 has "ZZ" in it then it's okay to delete rows 2 & 3.
Cells C4 & C5 have the same data in them BUT E4 nor E5 have "ZZ" in
them so I want the code to leave them alone.
 
D

Don Guillett Excel MVP

Adding the data from the different columns into one column doesn't
help.
Let's say cell C1 has "ABC" in it and E1 has "AA"
                    C2 has "DEF" in it and E2 has "BB"
                    C3 has "DEF" in it and E3 has "ZZ"
                    C4 has "GHI" in it and E4 has "CC"
                    C5 has "GHI" in it and E5 has "DD"
I want the code to recognize that C2 & C3 have the same data in them
AND because E3 has "ZZ" in it then it's okay to delete rows 2 & 3.
Cells C4 & C5 have the same data in them BUT E4 nor E5 have "ZZ" in
them so I want the code to leave them alone.- Hide quoted text -

- Show quoted text -

Solution based on seeing file. ZZ was left 2 chrs in col E string

Option Explicit
Sub DeleteRowsIfDuplicateAndZZinColE_SAS()
Application.ScreenUpdating = False
Dim i As Long
For i = Cells(Rows.Count, "c").End(xlUp).Row To 10 Step -1
If Cells(i + 1, "c") = Cells(i, "c") And _
UCase(Left(Cells(i + 1, "e"), 2)) = "ZZ" _
Then Rows(i).Resize(2).Delete
Next i
Columns.AutoFit
Application.ScreenUpdating = True
End Sub
 
D

Dave Peterson

Don's suggestion looks for consecutive rows that are duplicated.

This doesn't care if the rows are consecutive. Make sure you save your data
before you test -- it destroys the original.

And I'm confused about your original post. You loop through the bottom used row
of column C to row 1. But you use C10:C## in the countif range.

I'm gonna guess that this was a typo in your code and you're looking at all the
rows.

So this looks at all the data in column C one row at a time. If the value
appears in column C more than once, then it looks to see if there's a ZZ in
column E of any of the rows with that duplicated value.

If ZZ is found on any of those rows, then this procedure deletes all of those
duplicates.

So if we start with:

ABC AA
DEF BB
DEF ZZ
GHI CC
asdf DD
GHI CC
GHI ZZ

We'll finish with:

ABC AA
asdf DD


Both DEF's are deleted since ZZ was in E3.
All GHI's are deleted since ZZ was in E7.

Option Explicit
Sub testme()
Dim myColC As Range
Dim myColE As Range
Dim LastRow As Long
Dim wks As Worksheet
Dim myCountOfZZ As Long
Dim HowMany As Long
Dim iRow As Long
Dim DelRng As Range
Dim QtMarks As String
Dim myCVal As Variant
Dim myFormula As String

Set wks = ActiveSheet 'worksheets("Sheet1") '???

With wks
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
Set myColC = .Range("C1", .Cells(LastRow, "C"))
Set myColE = .Range("E1", .Cells(LastRow, "E"))

For iRow = LastRow To 1 Step -1
myCVal = .Cells(iRow, "C").Value
HowMany = Application.CountIf(myColC, myCVal)

If HowMany = 1 Then
'not a duplicate, so skip it
Else
If Application.IsNumber(myCVal) Then
QtMarks = ""
Else
QtMarks = """" 'surround strings with double quotes
End If
myFormula = "sumproduct(--(" & myColC.Address _
& "=" & QtMarks & myCVal & QtMarks & ")," _
& "--(" & myColE.Address & "=""zz""))"
myCountOfZZ = .Evaluate(myFormula)

If myCountOfZZ > 0 Then
If DelRng Is Nothing Then
Set DelRng = .Cells(iRow, "A")
Else
Set DelRng = Union(DelRng, .Cells(iRow, "A"))
End If
End If
End If
Next iRow
End With

If DelRng Is Nothing Then
'do nothing
Else
DelRng.EntireRow.Select
'use .delete when you're done testing!
'delrng.entirerow.select
End If

End Sub


The myFormula string looks like:

sumproduct(--($C$1:$C$20="ABC"),--($E$1:$E$20="zz"))

This is a way of counting the number of "ABC"'s in column C that have ZZ in
column E of the same row.

=sumproduct() likes to work with numbers. The -- stuff changes trues and falses
to 1's and 0's.

Bob Phillips explains =sumproduct() in much more detail here:
http://www.xldynamic.com/source/xld.SUMPRODUCT.html

And J.E. McGimpsey has some notes at:
http://mcgimpsey.com/excel/formulae/doubleneg.html
 
D

Don Guillett Excel MVP

Don's suggestion looks for consecutive rows that are duplicated.

This doesn't care if the rows are consecutive.  Make sure you save yourdata
before you test -- it destroys the original.

And I'm confused about your original post.  You loop through the bottomused row
of column C to row 1.  But you use C10:C## in the countif range.

I'm gonna guess that this was a typo in your code and you're looking at all the
rows.

So this looks at all the data in column C one row at a time.  If the value
appears in column C more than once, then it looks to see if there's a ZZ in
column E of any of the rows with that duplicated value.

If ZZ is found on any of those rows, then this procedure deletes all of those
duplicates.

So if we start with:

ABC             AA
DEF             BB
DEF             ZZ
GHI             CC
asdf            DD
GHI             CC
GHI             ZZ

We'll finish with:

ABC             AA
asdf            DD

Both DEF's are deleted since ZZ was in E3.
All GHI's are deleted since ZZ was in E7.

Option Explicit
Sub testme()
     Dim myColC As Range
     Dim myColE As Range
     Dim LastRow As Long
     Dim wks As Worksheet
     Dim myCountOfZZ As Long
     Dim HowMany As Long
     Dim iRow As Long
     Dim DelRng As Range
     Dim QtMarks As String
     Dim myCVal As Variant
     Dim myFormula As String

     Set wks = ActiveSheet 'worksheets("Sheet1") '???

     With wks
         LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
         Set myColC = .Range("C1", .Cells(LastRow, "C"))
         Set myColE = .Range("E1", .Cells(LastRow, "E"))

         For iRow = LastRow To 1 Step -1
             myCVal = .Cells(iRow, "C").Value
             HowMany = Application.CountIf(myColC, myCVal)

             If HowMany = 1 Then
                 'not a duplicate, so skip it
             Else
                 If Application.IsNumber(myCVal) Then
                     QtMarks = ""
                 Else
                     QtMarks = """"  'surround strings with double quotes
                 End If
                 myFormula = "sumproduct(--(" & myColC.Address _
                                   & "=" & QtMarks & myCVal & QtMarks & ")," _
                              & "--(" & myColE.Address & "=""zz""))"
                 myCountOfZZ = .Evaluate(myFormula)

                 If myCountOfZZ > 0 Then
                     If DelRng Is Nothing Then
                         Set DelRng = .Cells(iRow, "A")
                     Else
                         Set DelRng = Union(DelRng, .Cells(iRow, "A"))
                     End If
                 End If
             End If
         Next iRow
     End With

     If DelRng Is Nothing Then
         'do nothing
     Else
         DelRng.EntireRow.Select
         'use .delete when you're done testing!
         'delrng.entirerow.select
     End If

End Sub

The myFormula string looks like:

sumproduct(--($C$1:$C$20="ABC"),--($E$1:$E$20="zz"))

This is a way of counting the number of "ABC"'s in column C that have ZZ in
column E of the same row.

=sumproduct() likes to work with numbers.  The -- stuff changes truesand falses
to 1's and 0's.

Bob Phillips explains =sumproduct() in much more detail here:http://www..xldynamic.com/source/xld.SUMPRODUCT.html

And J.E. McGimpsey has some notes at:http://mcgimpsey.com/excel/formulae/doubleneg.html

Dave, my "simple" fully tested macro was based on the FILE SENT TO
ME......where OP asked to remove both duplicates if one of the entries
had the zz in col e.Comment from OP "This works great. "
 
D

Dave Peterson

Glad he got something that worked for him/her.



On 09/14/2010 16:59, Don Guillett Excel MVP wrote:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top