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
Related
I am trying to compare values from a range (e.g. column D) with values in a range beside (e.g. column C), which highlights the columns that has a different value in red, after which it moves right to compare the next range of values beside (i.e. Column E) with Column D and so on until there is a blank range of columns.
These are my codes. Application defined error.
Sub Macro1()
'
' Macro1 Macro
'
'
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Do Until IsEmpty(ActiveCell.Offset(, 1))
Selection.FormatConditions.Add Type:=xlExpression, Formulal:=ActiveCell.Select <> ActiveCell.Offset(0, -1).Value
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Loop
ActiveCell.Offset(0, 1).Select
End Sub
Try this - you need to construct a formula for the CF rule, using the Address() property of the first cell in the two ranges being compared. When you apply the rule the formula will adjust for each row, so no need to go cell-by-cell.
Sub Macro1()
Dim rngCF As Range, addr1, addr2, cf As FormatCondition
Set rngCF = Range(ActiveCell, ActiveCell.End(xlDown))
addr1 = rngCF.Cells(1).Address(False, False)
addr2 = rngCF.Offset(0, -1).Cells(1).Address(False, False) 'one column to left
Set cf = rngCF.FormatConditions.Add(Type:=xlExpression, _
Formula1:="=" & addr1 & "<>" & addr2)
With cf
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
End With
End Sub
In vba Find the value in column by Color RGB(255,199, 206) and Write the single Value as "Duplicate" in next column.
Please view the snap for more understanding.
I am trying below code to highlight the Cells
and then I wanted to use new code to print "duplicate" infront of highlighted cells .
Can you please guide me .
Sub Highlight_Duplicate_Value()
Columns("AD:AD").Select
Selection.FormatConditions.AddUniqueValues
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).DupeUnique = xlDuplicate
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
End Sub
Thank You
Something like this? Note this only checks interior color.
Sub colorcheck()
Dim Checkrange As Range
Dim cell As Range
lastrow = ActiveSheet.Cells(Rows.Count, "AD").End(xlUp).Row
Set Checkrange = ActiveSheet.Range("AD1:AD" & lastrow)
For Each cell In Checkrange
If cell.DisplayFormat.Interior.Color = 13551615 Then
cell.Offset(0, 1).Value = "Duplicate"
End If
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
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
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
...