Solved

Loop and highlight

Posted on 2012-04-12
11
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

623 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