Code To Highlight Row When Data Is In AA

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
Hi. I need a code please that will highlight a single row according to C when there is data in AA. As you can see below rows 2, 6, 7 ,8 and 11 are single rows (groups of more than one row can be ignored like 3-5 and 9-10)

Book1
CAA
1CatCode
2AUA314 1022Data
3AUA618 2012
4AUA618 2012
5AUA618 2012
6AUA618 3012
7AUA620 2008
8AUA620 2017Data
9AUA620 3008
10AUA620 3008
11AUA620 3014
Sheet1


Below you can see rows 2 and 8 have data in AA so need highlighting, rows 6, 7 and 11 have no data in AA so don't need highlighting. It will be used on various files up to 100,000 rows and there may be groups of rows matching in C up to about 20. I have hidden other columns for clarity.

Book1
CAA
2AUA314 1022Data
3AUA618 2012
4AUA618 2012
5AUA618 2012
6AUA618 3012
7AUA620 2008
8AUA620 2017Data
9AUA620 3008
10AUA620 3008
11AUA620 3014
Sheet1


Thanks.
 
So you want a highlight every time the first 6 characters of the data in column C changes?
Any unique cell in C, then needs to look at AA to see if there is data in it. If so highlight that row.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Any unique cell in C, then needs to look at AA to see if there is data in it. If so highlight that row.
I see you posted a different thread, with a similar question, I assume this is handled then?
 
Upvote 0
I see you posted a different thread, with a similar question, I assume this is handled then?
I did but the guy that solved that wouldn't help with this for some reason, and said to start another thread.
 
Upvote 0
I really didn't think this code was going to be too difficult for someone to do.
 
Upvote 0
Hey Dazz, without a sample workbook, and clear understanding of how the data is generated in the first place, this is what I came up with.
Assuming Column AB doesn't have data already in it, you need to add a "helper" column there to identify when the value in Column C changes (this identifies when a "new group" starts")
The formula to add to Cell AB2 is this
Excel Formula:
=IF(C2<>C1,"NEW","")


then insert this code into your workbook. this will only work when Values in Column AA are entered.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("AA:AA")) Is Nothing Then
     
  Dim c As Range, sh1 As Worksheet
  Set sh1 = Sheets("Sheet1")
  For Each c In sh1.Range("AB2", sh1.Cells(Rows.Count, "AB").End(xlUp))
    If c.Value <> "NEW" And c.Offset(-1, 0) <> "" Then
      c.Offset(-1, 0).EntireRow.Interior.ColorIndex = 6
    End If
  Next
  End If
End Sub
 
Upvote 0
Hey Dazz, without a sample workbook, and clear understanding of how the data is generated in the first place, this is what I came up with.
Assuming Column AB doesn't have data already in it, you need to add a "helper" column there to identify when the value in Column C changes (this identifies when a "new group" starts")
The formula to add to Cell AB2 is this
Excel Formula:
=IF(C2<>C1,"NEW","")


then insert this code into your workbook. this will only work when Values in Column AA are entered.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("AA:AA")) Is Nothing Then
    
  Dim c As Range, sh1 As Worksheet
  Set sh1 = Sheets("Sheet1")
  For Each c In sh1.Range("AB2", sh1.Cells(Rows.Count, "AB").End(xlUp))
    If c.Value <> "NEW" And c.Offset(-1, 0) <> "" Then
      c.Offset(-1, 0).EntireRow.Interior.ColorIndex = 6
    End If
  Next
  End If
End Sub
Thanks but I can't have a helper column in my data, sorry to be a pain
 
Upvote 0
Also I will be wanting to put the code in my personal macro workbook to use on hundreds of different files, so adding helper columns is not viable.
 
Upvote 0
Can you explain why VBA is necessary when Conditional Formatting can almost certainly do what you need?
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1CatCode12345678910111213141516171819202122232425More
2AUA314 1022Data
3AUA618 2012
4AUA618 2012
5AUA618 2012
6AUA618 3012
7AUA620 2008
8AUA620 2017Data
9AUA620 3008
10AUA620 3008
11AUA620 3014
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:AA11Expression=$AA2<>""textNO

And BTW, that will work regardless of how many columns you add or removed between A and AA. If you don't want the columns between A and AA to be highlighted, just adjust the Applies To range.
 
Upvote 0
Can you explain why VBA is necessary when Conditional Formatting can almost certainly do what you need?
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1CatCode12345678910111213141516171819202122232425More
2AUA314 1022Data
3AUA618 2012
4AUA618 2012
5AUA618 2012
6AUA618 3012
7AUA620 2008
8AUA620 2017Data
9AUA620 3008
10AUA620 3008
11AUA620 3014
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:AA11Expression=$AA2<>""textNO

And BTW, that will work regardless of how many columns you add or removed between A and AA. If you don't want the columns between A and AA to be highlighted, just adjust the Applies To range.
Because I've asked for a macro. As I've said I have hundreds of files with hundreds of thousands of rows. Formulas and conditional formatting isn't a user friendly option.
 
Upvote 0
Hi @Dazzawm.

will highlight a single row according to C when there is data in AA
I hope I understand your request correctly, try the following code:

VBA Code:
Sub Highlight_Row()
  Dim dic As Object, rng As Range, a, ky, i&
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("C1:AA" & Range("C" & Rows.Count).End(3).Row).Value2
  Set rng = Range("C1")
  
  For i = 2 To UBound(a, 1)
    If Not dic.exists(a(i, 1)) Then
      'Only consider when there is data in AA
      If a(i, 25) = "" Then dic(a(i, 1)) = 0 Else dic(a(i, 1)) = i
    Else
      dic(a(i, 1)) = 0
    End If
  Next
  
  For Each ky In dic.keys
    If dic(ky) <> 0 Then
      Set rng = Union(rng, Range("A" & dic(ky) & ":AA" & dic(ky)))
    End If
  Next
  rng.Interior.Color = vbYellow
  Range("C1").Interior.ColorIndex = xlNone
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top