Solved

Loop and highlight

Posted on 2012-04-12
11
354 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…

759 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

19 Experts available now in Live!

Get 1:1 Help Now