Solved

Loop and highlight

Posted on 2012-04-12
11
356 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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