Highlight certain rows - excel

I am trying to select certain rows in a pivot table and highlight them using vba. I have been trying a few things, but I have only been able to highlight one cell. The below code isn't working, but maybe a few tweaks to it will fix it. I need to do this in vba and not use conditional formatting.
Edit: Improved code using suggestion from answer below and own knowledge. It is still not working correctly though.
Sub Highlight()
Dim fnd As Variant
fnds = Array("abc", "dfy", "zxc")
For i = 0 To UBound(fnds)
Cells.Find(What:=(fnds), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
With Selection.EntireRow.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Next
End Sub

by using .Find you are only finding the first instance of each element of the array fnds, is this what you are wanting? I think you are after every occurrence in which case we need to put a loop in there.
Also personal preference here but I prefer to create reference to the cells / rows / columns to be manipulated then only do the actual manipulation once. Not such a biggy when you are only shading but when making changes such as deletions and updates it can be a massive process time saver.
Sub Highlight()
Dim i As Long, DelRange As String
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If InStr(1, Cells(i, 1), "abc") <> 0 Or InStr(1, Cells(i, 1), "dfy") <> 0 Or InStr(1, Cells(i, 1), "zxc") <> 0 Then DelRange = DelRange & "," & i & ":" & i
Next i
With Range(Right(DelRange, Len(DelRange) - 1)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub

The problem is the Rows collection you are making reference to. Instead you probably want EntireRow of the selected range.
With Selection.EntireRow.Interior
.Pattern = xlSolid
...

Related

Highlighting Blank Cells within Range

I am working on a macro that copies, pastes, and then creates templates forms of various sizes. Before the macro saves the template sheet as a separate file, I have it searching through a range - typically through D14:G end of data range - and highlights blank cells in a custom color. However, I have one very specific use case where there are no blank cells within the range (D14:G16), so it has been selecting all blank cells below this range (A17 to end of sheet). Can anyone help me work past this? Below is the excerpt from the macro that highlights the blankcells:
Set rLastCell = Sheets("Diversity Form").Cells.find(What:="*", After:=Sheets("Diversity Form").Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
'ColumnLetter2 = Split(Cells(1, rLastCell.Column).Address, "$")(1)
lCol = Sheets("Diversity Form").Cells(Rows.count, 4).End(xlUp).Row
'Dim ColumnLetter As String
'color only blank cells
For h = 4 To 7
ColumnLetter = Split(Cells(1, h).Address, "$")(1)
Let item = ColumnLetter & "14:G" & lCol
Sheets("Diversity Form").Range(item).SpecialCells(xlCellTypeBlanks).Select
On Error Resume Next
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
Next
No need to loop here, or to mess with column letters, or to Select.
Use WorksheetFunction.CountBlank first to test if there are any blanks.
With Sheets("Diversity Form")
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, 4).End(xlUp).Row
Dim checkRange As Range
Set checkRange = .Range(.Cells(14, 4), .Cells(lastRow, 7)) ' Or .Range("D14:G" & LastRow)
End With
If WorksheetFunction.CountBlank(checkRange) > 0 Then
With checkRange.SpecialCells(xlCellTypeBlanks).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End If

Problems getting "with selection" to work

I'm working on a excel vba code to import and manipulate some data from CSV-file. Suddenly a part of my code didn't work any more though it had worked without problems before.
It is about range.select and afterward with selection.Interior.Pattern = xlSolid
I have tried to copy the same small part of the code to a different workbook and here it work just perfect.
Dim iPhase As Integer
iPhase = Application.WorksheetFunction.CountIf(Range("A:A"), "Phase")
Dim h As Integer
h = 1
Range("A6").Select
Do Until h > iPhase
Cells.Find(What:="Phase", after:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveSheet.Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
With selection.Interior
.Pattern = xlSolid
.Interior.PatternColorIndex = xlAutomatic
.Interior.ThemeColor = xlThemeColorAccent6
.Interior.TintAndShade = 0
.Interior.PatternTintAndShade = 0
End With
With selection.Font
.Bold = True
End With
h = h + 1
Loop
I get a compile error: Expected function or variable #"selection.interior"
The comments already identify the issues with your code; but here is an alternative using Filter and SpecialCells to select the visible data. Comments are contained in the code.
Sub FliterWithConditionalFormatting()
Dim rng As Range
'properly defing and reference your workbook and worksheet, change as requiried
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1").CurrentRegion
'The WITH..END WITH statement allows you to shorten your code and avoid using SELECT and ACTIVATE
With rng
.AutoFilter Field:=1, Criteria1:="Phase", Operator:=xlAnd 'filter the rng
'set the range, to conditionally format only the visible data, skipping the header row
With .Range(Cells(2, 1), Cells(rng.Rows.Count, 17)).SpecialCells(xlCellTypeVisible)
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = xlThemeColorAccent6
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With .Font
.Bold = True
End With
End With
.AutoFilter 'Remove the filter
End With
End Sub

VBA ActiveCell Dynamic Resize

Hi there i have create the following code to format the Grand Total Row in excel spreadsheet. My problem is that I want to select dynamic the cells from Grand Total and right because I don’t have always 15 columns. I make a try with ActiveCell but it didnt work. Can anyone help me to change this code to fit my need?
Range("A1").Select
FindRow1 = Range("A:A").FIND(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole).Activate
ActiveCell.Resize(, 15).Select
'Range(ActiveCell, Cells(, ActiveCell.End(xlToRight).Column)).Select
Selection.Font.Bold = True
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
Selection.Font.Size = 12
[EDIT]: Here's a screenshot of my problem after trying suggested solutions:
You don't have to select the range. Just be sure of the address of the range you're using, and you're good.
It'd be better if you specify the worksheet you're working with, so
if you have multiple sheets in the workbook you'd still be working
on the right one.
Instead of activating the cell you found with Find, pass the row of that cell to a variable called myRow and use this variable in another function to define the range you need.
Once you have defined the range you need, pass it to a variable like myRange, and use it instead of using Selection in the rest of your code.
To make your range change its size dynamically (assuming you want your range to have one row and all the filled cells of that row), then you'll need to find the column of the last filled cell in your table, pass it to a variable lastCol and use it to define your range.
Sub formatRange()
Dim ws As Worksheet
Dim myRow As Long, lastCol As Integer, myRange As Range
Set ws = ThisWorkbook.ActiveSheet 'Change this to the name of the sheet you're working with
myRow = ws.Range("A:A").Find(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole).Row 'The row that has "Grand Total"
lastCol = ws.Cells(myRow, Columns.Count).End(xlToLeft).Column 'The column of the last filled cell in `myRow`
Set myRange = ws.Range("A" & myRow).Resize(1, lastCol) 'The desired range has 1 row and (lastCol) columns
myRange.Font.Bold = True
With myRange.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
myRange.Font.Size = 12
End Sub
Consider this case:
If the column of the last cell in the row myRow is NOT the same as the last column in the whole table (see screenshot), you have 2 choices to define your lastCol:
You can define lastCol as the last column of the row myRow (screenshot 1), and in that case you keep the code above as it is.
You can define it as the last column of the whole table (screenshot 2), and in that case you'd have to replace the lastCol line above with this:
'The column of the last filled cell in the whole table
lastCol = ws.Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
P.S, If the column of the last cell is the same in all your rows, you can ignore this last paragraph.
Another example as to how to avoid Select/Selection/Activate/ActiveXXX pattern and how to use nested With...End With structure:
With Range("A1", Range("A:A").Find(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole).End(xlToRight))
With .Font
.Bold = True
.Size = 12
End With
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
End With

Find the Difference between two rows and Highlight the difference then loop through all Rows with the Same Code

I Set up a Macro that Finds the differences between the two rows and then highlights them. I want the macro to Cycle through the next two rows and do the same thing and go on until there are no more rows of data(This Number varies all the time). So the Next selection would be Rows 4:5 and it would Select the differences and highlight them and so on. How is this possible? Any help is greatly appreciated. Thank you,
FindVariance Macro
Rows("2:3").Select
Range("A3").Activate
Selection.ColumnDifferences(ActiveCell).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("F16").Select
End Sub
Try:
FindVariance Macro
For j=2 to Range("A1").End(xlDown).Row-1
i=j+1
Rows(j & ":" & i).ColumnDifferences(Range("A" & i)).Offset(1,0).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
j=j+1
Next j
End Sub
Here's my sample
Option Explicit
Sub FindVariance()
Dim last As Integer, i As Integer, r As Boolean
last = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
For i = 2 To last
If i Mod 2 = 0 Then
Rows(i & ":" & i + 1).Select
r = Selection.ColumnDifferences(ActiveCell).Select
If r = True Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End If
Next i
Range("F16").Select
End Sub
it's always a good habit to:
use objects reference and avoid the use of selections
which can be deceiving and slows down the code
use full reference for ranges, up to the workbook.
to avoid point to an unwanted active sheet or workbook!
so here's my code
Sub FindVariance()
Dim j As Long
Dim nRows As Long
With ActiveSheet
nRows = .Cells(.Rows.Count, 1).End(xlUp).Row
For j = 2 To nRows Step 2
With .Rows(j).Resize(2).ColumnDifferences(.Cells(j + 1, 1)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Next j
End With
End Sub
and there's still some job to do in order to catch and properly treat exceptions (uneven number of rows, empty rows...)

IF Statement Formatting, highlighting row range

Hello again the last time I posted a question it was resolved quite quickly.
Essentially what I am trying to do is if a row in column r says Project Complete then I want it to grey out the row from column D:BM
I was playing around with a few things but it doesn’t want to work.
The column r is also a vlookup formula to another workbook, not sure if that makes a difference.
Dim rng As Range, cell As Range
Set rng = Range("R10:R1000")
For Each cell In rng
If cell.Value = "Project Complete" Then
Range("D" & ActiveCell.Row & ":BM" & ActiveCell.Row).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
End If
Next cell
You can loop through the range without selection:
Sub OhYa()
Dim rng As Range, c As Range
Set rng = Range("R10:R1000")
For Each c In rng.Cells
If c = "Project Complete" Then
With Range("D" & c.Row & ":BM" & c.Row).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
End If
Next c
End Sub

Resources