How to extract cells of a particular colour in pivot table? - excel

I have applied a conditional formatting to a column of a pivot table in Excel, which automatically colours the cells greater than a particular value in red.
I also want to extract those red cells separately to a specific area in the same Sheet.
Can it be done by using macros or an 'if' condition would help here?

What is your conditional formatting parameter? You could use the same logic programmatically. Alternatively you could write the code to check the color of the cell.
'specify the cell you want the color for
x = InputBox("what cell")
Range("a1") = Range(x).Interior.Color
If Range(x).Interior.Color = Range("a1") Then
MsgBox "yes the color is right"
End If
for a pivot table example
'where cell is something your looking for that is in the cell
ActiveSheet.PivotTables("PivotTable1").PivotSelect "'cell'", xlDataAndLabel + xlFirstRow, True
x = Selection.Interior.Color

Related

Uncolour past dates and colour only the current one

I have created a VBA code which displays with a blue colour the entire column where the today's date is located. The problem I have now is that my code does not uncolour the past dates. That means the colour of the past dates and the today's date is the same. I want only one column to be coloured which is the column of the today's date. I do not want for the past dates to have the same colour with the today's date. Current code below:
Any ideas?
Private Sub Workbook_Open()
Dim CellToShow As Range
Worksheets("Sheet2").Select
x = Day(Date)
Set CellToShow = Worksheets("Sheet2").Rows(3).Find(What:=x, LookIn:=xlValues) 'my dates are located across row 3
CellToShow.EntireColumn.Interior.Color = RGB(151, 228, 255) 'background colour in the selected cell
If CellToShow Is Nothing Then
MsgBox "No Cell for day " & x & " found.", vbCritical
Else
With CellToShow
.Select
.Show 'Scroll the window to show the cell
End With
End If
End Sub
EDIT:
Here is the image of the problem.As you can see, Excel does put a colour in the column that contain the today's date. But also already colours the yesterday's date. I do not want yesterday's date been coloured.
https://i.stack.imgur.com/oBf9P.png
Use Conditional formatting - set 'Applies To' to be the whole worksheet $1:$1048576, and the formula to be =A$3=DAY(TODAY()). All the vba has to do is the message box if no relevant date is found, and scroll to the relevant column.
EDIT:
The reason this will work is because conditional formatting formulas are written for the first (top-left) cell in the range to be formatted (so for the whole sheet, this formula is written how it would apply to cell A1. The formula is then applied to other cells in the range in the same relative way - hence the use of A$3, the $ fixes it to always refer to row 3, while the A is left to change dynamically for each cell to be coloured. In this way the formula applies to whole columns, because the row reference is fixed.

Cell color change if no formula or manually overwritten

Is it possible to change a cell color in excel if that cell doesn't contains a formula anymore or if it is manually overwritten and loses its formula?
Yes, it is possible to change attributes of a cell (like color, font etc.) without modifying the cell's value or formula. One way is to use conditional formatting as suggested in the comment section. If you want to use vba, the code to modify the background color of cells is
Range("A1").Interior.ColorIndex = colorCode 'You can replace range also by other range-objects like cells(1, 1)
You can find the colorCode for your specific color and more information about this formula here: ColorIndex property
What you want is to iterate through all cells and check whether the cell has a formula or not.
for i = 1 to x 'replace x by the number of rows you have
if (cells(i, y).isformula = false) then 'replace y by the column you want to search
cells(i, y).Interior.ColorIndex
End If
Next

Conditional Formatting of Cells to the LEFT/RIGHT of Cells that Contain Specific Text Beginning With

I'm looking for a formula or VBA code that can apply conditional formatting, specifically the fill color, to blank cells to the left or right of cells that contain specific text that begins with: "P". I've attached a screenshot of exactly what I'm looking to achieve.
Excel Screenshot
I want the fill color of cells in column A to change to blue if the cell next to it in column B contains text beginning with "P" and Pink if the the text is "T", etc.
The way I had achieved this before was with a macro to copy the data from column F and paste it into columns A,G,M then apply conditional formatting to those columns with the rule: Cell Value begins with "P", and then changing the fill color and text to the same color so as to appear solid.
It's hacky but works, unfortunately when printing, the cell's text will still show up against the fill background color. Which isn't the end of the world but am wondering if there is a more correct way to achieve this. Is there a way to offset which cells the formatting applies to? Any help is appreciated thank you!
Select columns A, F and M with F1 as the Active Cell. Create a new CFR using the following formula.
=or(iferror(left(e1)="P", false), left(g1)="P")
The problem with creating this in VBA is the formula; there is no column to the left of column A and any union of the three columns will always treat A1 as the 'active cell' regardless of how the union is created. .Range("G:G, M:M, A:A,") is the same as .Range("A:A, G:G, M:M"); A1 is the 'active cell'. One solution would be to temporarily switch to xlR1C1 where RC[-1] can be used to reference the non-existent column to the left of column A.
Option Explicit
Sub meh()
Dim refStyle As Long, xlR1C1formula As String
'store original reference style
refStyle = Application.ReferenceStyle
'make it xlR1C1 reference style
Application.ReferenceStyle = xlR1C1
With Worksheets("sheet1")
With .Range("A:A, G:G, M:M")
.FormatConditions.Delete
xlR1C1formula = "=or(iferror(left(rc[-1])=char(80), false), left(rc[1])=char(80))"
With .FormatConditions.Add(Type:=xlExpression, Formula1:=xlR1C1formula)
.Interior.ColorIndex = 5
.NumberFormat = ";;;"
End With
xlR1C1formula = "=or(iferror(left(rc[-1])=char(84), false), left(rc[1])=char(84))"
With .FormatConditions.Add(Type:=xlExpression, Formula1:=xlR1C1formula)
.Interior.ColorIndex = 22
.NumberFormat = ";;;"
End With
End With
'switch back
Application.ReferenceStyle = refStyle
End With
End Sub
The first thing I would do is make a Defined Name through Formulas > Defined Names > Define Name, called Here, with the following formula:
=INDIRECT(ADDRESS(ROW(),COLUMN(),4))
Then, under Home > Styles > Conditional Formatting > New Rule, use this formula on the cells you want to be affected:
=IF(AND(Here="",OR(LOWER(LEFT(OFFSET(Here,,1),1))="p",LOWER(LEFT(OFFSET(Here,,-1),1))="p")),TRUE,FALSE)

Excel - Format cells in a row if less than date in another cell

Ok, I'm wanting my excel spreadsheet to format cells in a row if the date is less than another cell.
e.g.
Cells B40,C40, D40,etc turn red if less than A40
Cells B41,C41, D41 etc turn red if less than A41
I'd like to be able to drag the formatting into cells below.
http://i.stack.imgur.com/VKNPE.jpg
on the home tab, go to "Conditional Formatting". Then click on New Rule. Then select the "use formula" option.
type approximately the following formula (in this case, your selection is B40):
=(B40<$A40)
and then apply a format to your choosing.
After finishing to apply the conditional format, you can copy-paste the format from B40 to the other cells (which will work, due to the $ sign before the A).
You can format your first row using conditional formatting.
Conditional formatting -> Highlight Cells Rules -> Less Than
And then using small macro you can copy it to your selecting row/s.
Sub NewCF()
Range("B1:D1").Copy
For Each r In Selection.Rows
r.PasteSpecial (xlPasteFormats)
Next r
Application.CutCopyMode = False
End Sub
It does this by copying the format of the first row to EACH row in the selection (one by one, not altogether). Replace B1:D1 with the reference to the first row in your data table.

Change the color of a row based on string data in a cell using VB in excel

I have an excel spreadsheet where I would like to highlight all the rows where the string "05 Paint" appears in cells in column A. I found this code that changes the color of the cell containing the string "05 Paint" but can't figure out how to highlight the whole row:
Sub Highlight()
Dim rng As Range, cell As Range
Set rng = Range("A2:A250")
For Each cell In rng
Select Case cell.Value
Case "05 Paint"
cell.Interior.ColorIndex = 4
End Select
Next
End Sub
Ideally I would like the row to be highlighted from column A to column I.
Any help would be appreciated.
Thanks!
Use
Cell.Resize(1, 9).Interior.ColorIndex = 4
So you know which row to be highlighted. That makes it none-dynamic.
You can try "conditional formatting" for that row. Plus going through each cell could be as nearly costly as my suggestion.
You can try and compare the best performance gain solutiin for your need.
Where "entire row" has been defined a ColumnsA:I, the search string is 05 Paint (being either all of or part of the cell content) and the search is case insensitive then:
Record Macro:
select ColumnsA:I, HOME > Styles - Conditional Formatting, New Rule...
Use a formula to determine which cells to format
Format values where this formula is true:
=SEARCH("05 Paint",$A1)
Format..., select formatting, OK, OK.
Stop recording.

Resources