Solved

Loop and highlight

Posted on 2012-04-12
11
355 Views
Last Modified: 2012-04-18
Hi,

I need a piece of code that counts Col A as its reference for how many rows to loop through and then starts looping down.

If it finds that there are rows which have the same value in both columns F&G, then it will highlight those rows red

I have attached a spreadsheet which shows what i would like the ss to look like after the code has ran

Thanks
Seamus
test.xls
0
Comment
Question by:Seamus2626
  • 6
  • 3
  • 2
11 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 37837409
Hi, Seamus.

This could be done with Conditional Formatting. Beside simplicity, the main advantage is that the highlighting would be automatically updated if either of the values changed,

Interested?

Regards,
Brian.
0
 

Author Comment

by:Seamus2626
ID: 37837418
Yep!
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37837449
Seamus,

My mistake - I didn't check your file adequately. I had thought you were looking to highlight individual rows where columns F and G were the same.

You're actual requirements can be done by Conditional Formatting, but I'd be concerned that there would be an excessive processing overhead. How many rows will you have and how volatile will the data be?

Finally, by counting column A as the reference, do you mean to only include rows where column A is non-blank?

Thanks,
Brian.
0
 

Author Comment

by:Seamus2626
ID: 37837462
There could easily be a few hundered rows of data

Yes, if Column A ends at row 75, only loop as far as 75

Thanks
Seamus
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37837998
Seamus,

OK, quick and dirty...
Option Explicit

Sub HighLight_Duplicates()
Dim i As Long
Dim xLast_Row As Long
Dim xLast_Col As Long
Dim xCount

Sheets("Sheet1").Activate

xLast_Col = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Column
xLast_Row = ActiveSheet.Range("A1").End(xlDown).Row
If xLast_Row < 2 Then
    MsgBox ("No data found. Run cancelled.")
    Exit Sub
End If

Range(Cells(2, 1), Cells(xLast_Row, xLast_Col)).Interior.Color = 16777215

Range(Cells(2, xLast_Col + 1), Cells(xLast_Row, xLast_Col + 1)).Formula = "=F2&"":""&G2"
Range(Cells(2, xLast_Col + 2), Cells(xLast_Row, xLast_Col + 2)).Formula = "=IF(A2="""","""",COUNTIF($M$2:$M$" & xLast_Row & ",M2))"

For i = 2 To xLast_Row
    If Cells(i, xLast_Col + 2) <> 1 Then Range(Cells(i, 1), Cells(i, xLast_Col)).Interior.Color = 255
Next

Range(Cells(2, xLast_Col + 1), Cells(2, xLast_Col + 2)).EntireColumn.Delete
                   
End Sub

Open in new window

Regards,
Brian.test-V2.xls
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 37838118
Seamus,

Few points...
(1) As with the previous version, highlighting terminates on the row before the first blank cell in column A.
(2) I don't know if you ever re-run the macro for a sheet, so at the start of the run, I reset the highlighting for all rows (i.e. including and past the last non-blank cell in column A).
(3) The previous version didn't handle re-runs too well. This is now fixed.

As a test, please run the macro, clear the contents of A8 and re-run the macro. Are you happy with the final results?

New code is...
Option Explicit

Sub HighLight_Duplicates()
Dim i As Long
Dim xLast_Row As Long
Dim xLast_Col As Long
Dim xCount

Sheets("Sheet1").Activate

xLast_Col = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Column
xLast_Row = ActiveSheet.Range("A1").End(xlDown).Row
If xLast_Row < 2 Then
    MsgBox ("No data found. Run cancelled.")
    Exit Sub
End If

Range(Cells(2, 1), Cells(ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row, xLast_Col)).Interior.Color = 16777215

Range(Cells(2, xLast_Col + 1), Cells(xLast_Row, xLast_Col + 1)).Formula = "=F2&"":""&G2"
Range(Cells(2, xLast_Col + 2), Cells(xLast_Row, xLast_Col + 2)).Formula = "=IF(A2="""","""",COUNTIF($M$2:$M$" & xLast_Row & ",M2))"

For i = 2 To xLast_Row
    If Cells(i, xLast_Col + 2) <> 1 Then Range(Cells(i, 1), Cells(i, xLast_Col)).Interior.Color = 255
Next

Range(Cells(2, xLast_Col + 1), Cells(2, xLast_Col + 2)).EntireColumn.Delete

Debug.Print ActiveSheet.UsedRange.Count
                   
End Sub

Open in new window

Regards,
Brian.test-V3.xls
0
 

Author Closing Comment

by:Seamus2626
ID: 37838492
Thats perfect Brian

Thanks
Seamus
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37838510
Thanks, Seamus.
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37855762
Hi,

Can someone explain this?

Range(Cells(2, 1), Cells(ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row, xLast_Col)).Interior.Color = 16777215

&

Range(Cells(2, xLast_Col + 1), Cells(2, xLast_Col + 2)).EntireColumn.Delete

Why do you need above lines?

Thanks,
Ashok
0
 
LVL 26

Expert Comment

by:redmondb
ID: 37863388
Hi, Ashok.

(1) This is to remove any existing highlighting as the user may wish to re-run the macro after changing/deleting some rows.

(2) The main work is done by the formulas in the two new columns. Once the formatting has been applied they're no longer needed and so they're deleted.

Regards,
Brian.
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37863630
Brian,

Now it makes sense.

Thank you

Ashok
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro 6 49
macro for filtering the criteria and sending mail to all the data 8 27
Boolean help 6 27
ADD New Entries 7 16
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now