Currently I am using Data Validation to select from a list of options a cell can be. Is it possible to do the same thing but with styles? I.e. from the dropdown I can select a subset of styles I would like to apply to the cell?
Add some code to change the format of some cells, based on the value change of certain cell.
Let's say that you want to change the color of cell D2 based on the value of cell B2 (both in Sheet1). Open your VBA editor, double click on "Sheet1" of your book in your Project browser and select the Worksheet object and the Change procedure, and do something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then ' This is the address of the cell being changed
Select Case Target.Value
Case 1 ' The changed cell value is 1
ThisWorkbook.ActiveSheet.Range("d2").Interior.Color = RGB(125, 125, 125)
Case 2 ' The changed cell value is 2
ThisWorkbook.ActiveSheet.Range("d2").Interior.Color = RGB(125, 125, 255)
Case 3 ' The changed cell value is 3
ThisWorkbook.ActiveSheet.Range("d2").Interior.Color = RGB(125, 0, 255)
End Select
End If
End Sub
You can google around to find ways to change other format properties of the cell.
Related
Good afternoon,
I have cells the first has a date and the second has a date, and the third checks the first two. If the first cell has a value it returns "OPEN", if the second cell has a value it instead returns "WAITING",
eg. Cell F4 "=IF(D4=0,"",IF(AND(D4>0,E4=0),"OPEN","WAITING"))"
and then I put a date in that cell replacing the formula.
Then another cell checks the whole column and if it contains "OPEN" or "WAITING" it's value is "OPEN", if not it's value is "CLOSED",
eg. Cell M2 "=IF(SUM(COUNTIF(F2:F1000,{"OPEN","WAITING"})),"OPEN","CLOSED")"
Then I have VBA on the tab colour, which changes to either green if that cell is "CLOSED" or red if it's "OPEN".
eg.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M$2" Then
Select Case Target.Value
Case "OPEN"
Me.Tab.Color = vbRed
Case "CLOSED"
Me.Tab.Color = vbGreen
Case Else
Me.Tab.Color = vbOrange
End Select
End If
End Sub
This all works except for the tab colour. I have to open the M2 cell (which has calculated correctly) and press enter to activate the tab colour change.
The cells that are checking previous cells are not always column F, some are different. The final value is always M2 in every sheet.
The reason I tried to do this was so I wouldn't need to change the tab colour manually. Is there a better way to do this? I'm currently achieving no automation, if anything it's slightly more work.
I have a macro to colour a cell based on the RGB values.
I have cells A1, B1, and C1 for the RGB values.
Sub FillWithRBG()
Range("D1").Interior.Color = RGB(Range("A1").Value, Range("B1").Value, Range("C1").Value)
End Sub
I can select anywhere in the worksheet, run the macro and only cell D1 will change colour.
I want to select cell D2, run the macro and Cell D2 changes colour based off values in A2, B2, and C2.
I imagine I need to set the active cell with a reference, then 1, 2 and 3 will be offset from the selected cell.
An added bonus would be that the macro can only run in the D column to prevent errors.
This would be a simple approach
In the code Me. is refering to the current sheet
You have to place this code in the sheet's module
Columns in VBA are noted by numbers (so column A is referenced as column 1)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Prevent change if changed values are no in these columns (1,2,3) numbers are equivalent to A, B, C
If Target.Column > 3 Then Exit Sub
' Change color of D column (4 = D)
Me.Cells(Target.Row, 4).Interior.Color = RGB(Me.Cells(Target.Row, 1).Value, Me.Cells(Target.Row, 2).Value, Me.Cells(Target.Row, 3).Value)
End Sub
If you want it to work only if you're changing one cell at a time add these lines:
' Prevent change when more than one cell is changed
If Target.Cells.Count > 1 Then Exit Sub
I'm relatively new to excel and need some advice. I'm using excel 2016. I have read through other posts and can't find anything that really matches. Does anyone know how to get a cell in a row to change to todays date only when any other cell in that row is changed? And secondly if that date in that cell is more than week old and the value of another cell in the same row is "open" then change the fill color? thanks for any insight you can provide.
You'll need some VBA for this. Get into Visual Basic Editor (VBE) with Alt+F11. In your VBAProject (your workbook in the Project window) double click the sheet that contains the cell in which you are wanting to detect a change. Then add the following::
Private Sub Worksheet_Change(ByVal Target As Range)
'Detect if cell A1 has had a change
If Not Intersect(Target, Range("A1")) Is Nothing Then
'Change cell B2 to todays date
Range("B2").Value = Now()
'Detect if value in cell C2 is changed to "Open"
ElseIf Not Intersect(Target, Range("C2")) Is Nothing Then
'Is the date in B2 older than a week from now?
If DateAdd("d", -7, Now()) > Range("B2").Value Then
'Change the cell color of B2
Range("B2").Interior.Color = RGB(0, 255, 255) 'Cyan... it's so beautiful
End If
End If
End Sub
That subroutine is a special subroutine that runs when any cell changes value in the worksheet in which the subroutine is placed. We detect if a change was made to a specific cell with that awful If Not Intersect(... line. Then it's pretty straight VBA to change values and colors of cells and do some date arithmetic and tests and whatnot.
I can format the cell to display Currency and Percentage, but can I have it display both in one cell depending on what is entered?
For example if I enter $5 it would display $5.00 and if I enter 5% it would display 5 %
If this possible?
The answer is NO if you wanted to do this with custom number formatting, all by itself.
This could be accomplished if you were OK with using VBA to set the custom number formatting at the time of value entry using the Worksheet_Change() event procedure.
Like so:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
If Not Intersect([a1:a10], Target) Is Nothing Then
Select Case True
Case Left$(Target.NumberFormat, 1) = "$": Target.NumberFormat = "$#,##0.00"
Case Target.NumberFormat = "0%": Target.NumberFormat = "0 %"
End Select
End If
End If
End Sub
Note the [a1:a10] near the top of the procedure. This specifies the range on the sheet that will respond to this dynamic formatting. Edit that range to your liking.
I have a floor plan with about 50 resources that either need to be marked available, reserved, or occupied. I chose to use data validation in-cell drop-down list for each resource. Is it possible to keep the text in the data validation cell, use the source as the drop-down options, and change the fill color based on the selection? That might be hard to understand so I'll provide an example.
Example given:
A1:A3 is the source with Available, Reserved, and Occupied.
Cell D3: Available, Reserved, and Occupied as drop-down list, the fill color will change to green, orange, or red respectively, and it will keep the text in the cell.
First place the ComboBox control and set the .visible property to false. using .AddItem method add Available and others. to its list. I think you want something like below.
Private mrngMyRange As Range
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = 4 Then 'This restricts the sensitive range to column `D`
ComboBox1.Visible = True
ComboBox1.Left = Target.Left
ComboBox1.Top = Target.Top
Set mrngMyRange = Target
ComboBox1.Select
Else
ComboBox1.Visible = False
End If
End Sub
Private Sub ComboBox1_Change()
'mrngMyRange.Value = ComboBox1.Text
If ComboBox1.Text = "Available" Then
mrngMyRange.Cells(1, 1).Interior.ColorIndex = 4
ElseIf ComboBox1.Text = "Reserved" Then
mrngMyRange.Cells(1, 1).Interior.ColorIndex = 46
ElseIf ComboBox1.Text = "Occupied" Then
mrngMyRange.Cells(1, 1).Interior.ColorIndex = 3
End If
ComboBox1.Visible = False
End Sub
First, use Data Validation to create the list in based on the values in cells A1:A3 (Available, Reserved, and Occupied) in cell D3.
Then, use Conditional Formatting to setup three rules through "Use a formula to determine which cells to format". You'll need one for each colour, ie.
=$D$3="Available" - Applies to: =$D$3 - Format: Green fill
=$D$3="Reserved" - Applies to: =$D$3 - Format: Orange fill
=$D$3="Occupied" - Applies to: =$D$3 - Format: Red fill
You can use Conditional Formatting to achieve the same. Example given below is using MS-Excel 2007.
Assuming that you have a dropdown already present at D3 (or multiple cells), select the cell (or cells) in which the formatting is required.
In Home tab -> go to Conditional Formatting option -> select Highlight Cell Rules -> Equal To option. A dialog box will open as shown in the image below. Enter the cell reference that you want and the format required (e.g. $A$1 for Available) and click OK.
Repeat the same for Reserved and Occupied as well with appropriate formatting.