My Workbook has several sheets named Detail1, Detail2, Detail3, and so on. I´d like to color the sheet according to the content of cell B15. However the code is only coloring sheet Detail1 and not cycling through the other sheets. What am I doing wrong?
Sub Color_by_cell_value()
Dim X As String
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name Like "Detail*" Then
X = Range("B15").Value
Select Case X
Case Is = "no"
Range("A1:AZ100").Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.799981688894314
End With
Range("B15").Select
With Selection.Interior
.Color = 5296274
End With
Case Is = "yes"
Range("A1:AZ100").Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.599993896298105
End With
Range("B15").Select
With Selection.Interior
.Color = 255
End With
Case Is = "yes/no"
Range("A1:AZ100").Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
Range("B15").Select
With Selection.Interior
.Color = 65535
End With
End Select
End If
Next ws
End Sub
There is lots of ways to edit this code and make it more efficient, but to answer your question. ws.select would be required after line.If ws.Name Like "Detail*" Then
For Each ws In Worksheets
If ws.Name Like "Detail*" Then
ws.Select
X = Range("B15").Value
Select Case X
You can also use ws.Activate:
Sub Color_by_cell_value()
Dim X As String
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In Worksheets
If ws.Name Like "Detail*" Then
ws.Activate
X = Range("B15").Value
Select Case X
Case Is = "no"
Range("A1:AZ100").Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.799981688894314
End With
Range("B15").Select
With Selection.Interior
.Color = 5296274
End With
Case Is = "yes"
Range("A1:AZ100").Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.599993896298105
End With
Range("B15").Select
With Selection.Interior
.Color = 255
End With
Case Is = "yes/no"
Range("A1:AZ100").Select
With Selection.Interior
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
Range("B15").Select
With Selection.Interior
.Color = 65535
End With
End Select
End If
Next ws
Application.ScreenUpdating = True
End Sub
Note I also set the Application.ScreenUpdating to False as a good custom.
Related
How could I edit my code to only highlight the row from column G:K instead of wasting memory and time highlighting the entire row?
With ActiveSheet 'set this worksheet properly!
'lastrow = .cells(Rows.Count, 1).End(xlUp).Row
lastrow = Range("K6500").End(xlUp).Row - 2
For Each cell In .Range("K3:K" & lastrow)
If cell = "Wrong Date" Then
'With cell.EntireRow.Interior
With cell.Range("G:K").Value.Interior.ColorIndex = 3
Rows().Select
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 3937500
.TintAndShade = 0
.PatternTintAndShade = 0
End With
My current code does not work as I've tried replacing With cell.EntireRow.Interior with With cell.Range("G:K").Value.Interior.ColorIndex = 3
Excuse me this is what I mean I am trying to do
Sub highlight_wrong_Date()
Dim Rng As Range, lCount As Long, lastrow As Long
Dim cell As Object
With ActiveSheet 'set this worksheet properly!
lastrow = Range("K6500").End(xlUp).Row - 2
For Each cell In .Range("K3:K" & lastrow)
If cell = "Wrong Date" Then
With cell.Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
Rows().Select
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 3937500
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ElseIf cell = "Pass" Then
With cell.Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
Rows().Select
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 61046
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Else
With cell.EntireRow.Interior
Rows().Select
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next cell
End With
End Sub
But I receive an error saying the cell object does not support this. If a cell has either a value of "Wrong Date" or "Pass" within column O I want to highlight red or green respectively.
3rd Edit
Sub highlight_wrong_Date()
Dim Rng As Range, lCount As Long, lastrow As Long
Dim cell_value As Object
With ActiveSheet 'set this worksheet properly!
lastrow = Range("K6500").End(xlUp).Row - 2
For Each cell_value In .Range("K3:K" & lastrow)
If cell_value = "Wrong Date" Then
With .Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
'Rows().Select
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 3937500
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ElseIf cell_value = "Pass" Then
With .Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
'Rows().Select
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 61046
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Else
With cell.EntireRow.Interior
Rows().Select
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next cell_value
End With
End Sub
Your references should be
With .Range("G" & cell.Row & ":K" & cell.Row)
I have a dataset in which each row belongs to a unique person so what I want to do is that find duplicate values in each row.
I tried using conditional formatting but its very time consuming as I have to apply it to each individual row otherwise it will find duplicates among all rows not just one row.
Could you please suggest something that can help me it can be formula or vba or formula for conditional formatting.
I used macro recorder to create a macro and the results is below. If I can make it go through a range of rows and apply the formatting that could help
Sub DuplicatesRow1() ' ' DuplicatesRow Macro '
'
Rows("251:251").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
Selection.FormatConditions(1).StopIfTrue = False
Rows("252:252").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
Selection.FormatConditions(1).StopIfTrue = False
Rows("253:253").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
Selection.FormatConditions(1).StopIfTrue = False
Range("E259").Select End Sub
I further worked on this and managed to come up with the following code which seems to be working for me. I am new to VBA and do not have enough experience so please let me know if my code can be improved further
Private Sub HighlightDuplicateRow(row As Integer)
Dim report As Worksheet
Set report = Excel.ActiveSheet
report.Cells(row, row).EntireRow.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
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Sub DuplicatesInEachRow()
Dim counter As Integer, limit As Variant
counter = 2
limit = InputBox("Give me last row number", "Highlight Duplicates in a Row")
If limit = "" Then Exit Sub
Do Until counter > limit
Call HighlightDuplicateRow(counter)
counter = counter + 1
Loop
End Sub
Here is a loop that will set a conditional format on each row. I used sheet and range references based on your sample data and code. You modify these to fit your exact data set.
I will also note that I am concerned about this causing performance issues in Excel if there are numerous rows, as the amount of Formats may seriously raise your file size and affect performance.
Sub LoopCF()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
'Dim lRow As Long
'lRow = ws.Range("A2").End(xlDown).Row 'will give row 200 as long as contiguous rows
Dim rng As Range, cel As Range
Set rng = ws.Range("B2:B200") 'ws.Range("B2:B" & lRow)
For Each cel In rng
With cel.Resize(1, 4)
.FormatConditions.AddUniqueValues
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1)
.DupeUnique = xlDuplicate
With .Font
.Color = -16383844
.TintAndShade = 0
End With
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
.StopIfTrue = False
End With
End With
Next
End Sub
I am trying to write a code that can apply conditional formatting to each intersection of two pivot fields separately. For example, I would like to select all entries for PartNumber 541-9037-100 in the "Average of TransA1" column, apply conditional formatting, then proceed to PartNumber, select all entries in the "Average of TransA1" column, apply conditional formatting, etc.
PartNumbers will vary from week to week, so I am looking for a generic code that will loop through every item in the PartNumber field.
I have tried to use a for loop, but seem to be selecting the entire Average of TransA1 column at once rather than the intersection of the Average of TransA1 column and each PartNumber item. A copy of my code is below- Can anyone help me revise my code to select only the intersections of the fields?
ConditionalFormatting2 Macro
'
' Select intersect of pivot table and output cell values to apply formatting
Dim pt As PivotTable
Set pt = Worksheets("Pivot Sheet").PivotTables("PivotTable2")
For Each PivotItem In pt.PivotFields("PartNumber").PivotItems
'Select the "Average of TransA1" column and apply conditional formatting
Application.PivotTableSelection = True
pt.PivotSelect "Average of TransA1", xlDataOnly
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = 30
.Percent = True
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = 10
.Percent = True
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 8420607
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Bottom
.Rank = 30
.Percent = True
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Bottom
.Rank = 10
.Percent = True
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 8420607
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Next PivotItem
'
End Sub
strong text
Figured it out- it isn't pretty, but it works
'declare variables
Dim pt As PivotTable
Dim rng1 As Range
Dim rng2 As Range
Dim pn As String
Dim rw As Integer
Dim cntpi As Long
'Sets current worksheet, initializes rw and cntpi
Set pt = Worksheets("Pivot Sheet").PivotTables("PivotTable2")
rw = 5
cntpi = 0
'sets rng1 as the Average of TransA1 column
Range("A5").Select
Set rng1 = pt.PivotFields("Average of TransA1").DataRange.EntireColumn
'Loops through each part number, applies conditional formatting to all part number groups in data area
For Each PivotItem In pt.PivotFields("PartNumber").PivotItems
rw = rw + cntpi
Range(Cells(rw, 1), Cells(rw, 1)).Select
pn = Trim(ActiveCell.Value)
Set rng2 = pt.PivotFields("PartNumber").PivotItems(pn).DataRange.EntireRow
Intersect(rng1, rng2).Select
cntpi = Selection.Rows.Count
Selection.FormatConditions.AddColorScale ColorScaleType:=3
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 6737151
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValuePercentile
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 6736896
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 6737151
.TintAndShade = 0
End With
Next PivotItem
I'm looking to run a series of conditional formatting rules on two columns of data. I know I can select a column letter and all the cells in that columns, but to make my macro more full-proof I'd like for it to search for the column's header and run the formatting on the cells below the two specific headers.
I Currently used the Record Macro tool to see how this could potentially work, and this is my current setup. This macro does work, I would just like to make it full-proof and search through the worksheet for the specific column title names. I'd like to search a woorksheet for the column Titles "Header 1" and "Header 2".
Sub TwoColumnConditional()
Columns("K:AJ").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT1", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT2", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16751204
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 10284031
.TintAndShade = 0
End With
End Sub
Try this:
Sub ApplyCF()
Dim ws As Worksheet
Dim rng As Range, fcel As Range, hrng As Range
Dim myheaders, header
Dim mycondition As String
myheaders = Array("Header 1", "Header 2")
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set rng = .Range("A1", .Range("A1").Offset(0 _
, .Columns.Count - 1).End(xlToLeft).Address)
'Debug.Print headers.Address
End With
For Each header In myheaders
Set fcel = rng.Find(header, rng.Cells(rng.Cells.Count))
If Not fcel Is Nothing Then
If hrng Is Nothing Then
Set hrng = fcel.EntireColumn
Else
Set hrng = Union(hrng, fcel.EntireColumn)
End If
End If
Next
'Debug.Print hrng.address
hrng.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT1", _
TextOperator:=xlContains
With hrng.FormatConditions(hrng.FormatConditions.Count)
With .Font
.Color = -16383844
.TintAndShade = 0
End With
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
.StopIfTrue = False
End With
hrng.FormatConditions.Add Type:=xlTextString, String:="CONTAINSTEXT2", _
TextOperator:=xlContains
With hrng.FormatConditions(hrng.FormatConditions.Count)
With .Font
.Color = -16751204
.TintAndShade = 0
End With
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 10284031
.TintAndShade = 0
End With
.StopIfTrue = False
End With
End Sub
I only assumed your headers are in the first row.
Adjust the code to suit.
I am trying to format cells in excel
Following is the code I have:
Range("A1:F1").Select
Selection.Font.Bold = True
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
I need to repeat the formatting steps multiple times for different ranges, can I call a subroutine instead?
Here is one way to apply the format to a number of ranges in a loop:
Sub dural()
Dim r As Range
ary = Array(Range("A1:Z1"), Range("A3:Z3"), Range("A7:Z7"))
For i = LBound(ary) To UBound(ary)
Call FFormat(ary(i))
Next
End Sub
Sub FFormat(rIn As Variant)
rIn.Font.Bold = True
With rIn.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub