Uncolour past dates and colour only the current one - excel

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.

Related

Is it possible to have excel circle the cell that contains today's date?

I have a big calendar spreadsheet that currently just highlights today's date with conditional formatting. However, I do not like any of the formats I could possibly apply and so I would love to be able to have something whereby a circle is drawn over the top of the cell with today's date in.
The way each day is laid out is so that (for example) A1 is the date itself in "dd" format and then A2 has the information in. The information is pulled through from an event data list so it has a formula in.
I have seen some stuff on this being possible with a VBA code but I am just not sure how to write that.
Thank you
Sam
This will draw an oval round today's date. If today's date is not found, an error message will be shown:
Sub DrawOval()
Dim cell As Range, circ As Shape
Set cell = Sheet1.Cells.Find(Date, Sheet1.Range("A1"))
If Not cell Is Nothing Then
Set circ = Sheet1.Shapes.AddShape(msoShapeOval, 187.8, 37.2, 63.6, 24)
With circ
.Select
Selection.ShapeRange.Fill.Visible = msoFalse
.Top = cell.Top
.Left = cell.Left
End With
Else
MsgBox "Cell with today's date not found!", vbCritical + vbOKOnly, "Error"
End If
End Sub
This assumes your worksheet name is Sheet1, so amend that accordingly. You can run this by adding a shape to your worksheet, and assigning this macro to it.

date in a cell to change when any cell in that row is modified

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.

Conditional fomatting based on date and whether cell is blank

Need some help with a spreadsheet that i have created as a timesheet rota. Basically i would like to highlight cells in the below calendar where the date is less than today and the cell is blank to identify when people have not entered the shift they have worked.
For example on the calendar i would like the top three rows where nothing has been submitted to highlight red
Is this possible?
Thanks in advance for the help!
Put this code in the worksheet module and adjust the range to suit what you need. Change this Range("A1:C10") to suit the ranges that you want affected.
The code will determin if the cell is empty or has a date before today and change the color to red.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:C10")) Is Nothing Then
If Target Is Nothing Then
Target.Interior.ColorIndex = 3
ElseIf Target <= Date Then
Target.Interior.ColorIndex = 3
End If
End If
End Sub
You could use conditonal formating. You have to do the following for every month.
Select a cell (for example E10). Goto [Condional Formating (on the Home tab) >> Highlight Cells Rules >> more Rules >> Use a formula to....].
Type
=AND(E$5<TODAY(),E10="")
in the textbox and choose a format (like highlighting red).
Then apply this formatting to all the cells in that particular month (Conditional Formatting >> Manage Rules)
Repeat this proces for the other months.

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

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

Excel Formula which places date/time in cell when data is entered in another cell in the same row

Hoping there is a way this can be done with a formula since I will be putting this on SharePoint as a shared workbook.
Column B contains Tasks, while Column E contains the Date and Time of when the Task was assigned. Is there a formula that would automatically enter the current date and time in Column E whenever someone entered data into column B?
Any assistance would be greatly appreciated.
Another way to do this is described below.
First, turn on iterative calculations on under File - Options - Formulas - Enable Iterative Calculation. Then set maximum iterations to 1000.
The 1000 iterations doesn't matter for this formula, but it stops excel getting stuck in an infinite loop for other circular references.
After doing this, use the following formula.
=If(D55="","",IF(C55="",NOW(),C55))
Once anything is typed into cell D55 (for this example) then C55 populates today's date and/or time depending on the cell format. This date/time will not change again even if new data is entered into cell C55 so it shows the date/time that the data was entered originally.
This is a circular reference formula so you will get a warning about it every time you open the workbook. Regardless, the formula works and is easy to use anywhere you would like in the worksheet.
This can be accomplished with a simple VBA function. Excel has support for a Worksheet Change Sub which can be programmed to put a date in a related column every time it fires.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Offset(0, 3).Value = "" Then
Target.Offset(0, 3) = Format(Now(), "HH:MM:SS")
End If
End Sub
A quick explanation. The following "if" statement checks for two things: (1) if it is the second column that changed (Column B), and (2) if the cell 3 columns over (Column E) is currently empty.
If Target.Column = 2 And Target.Offset(0, 3).Value = "" Then
If both conditions are true, then it puts the date into the cell in Column E with the NOW() function.
Target.Offset(0, 3) = Format(Now(), "HH:MM:SS")
Range.Offset
Range.Column
Not sure if this works for cells with functions but I found this code elsewhere for single cell entries and modified it for my use. If done properly, you do not need to worry about entering a function in a cell or the file changing the dates to that day's date every time it is opened.
open Excel
press "Alt+F11"
Double-click on the worksheet that you want to apply the change to (listed on the left)
copy/paste the code below
adjust the Range(:) input to correspond to the column you will update
adjust the Offset(0,_) input to correspond to the column where you would like the date displayed (in the version below I am making updates to column D and I want the date displayed in column F, hence the input entry of "2" for 2 columns over from column D)
hit save
repeat steps above if there are other worksheets in your workbook that need the same code
you may have to change the number format of the column displaying the date to "General" and increase the column's width if it is displaying "####" after you make an updated entry
Copy/Paste Code below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
Target.Offset(0, 2) = Date
End Sub
Good luck...
I'm afraid there is not such a function. You'll need a macro to acomplish this task.
You could do something like this in column E(remember to set custom format "dd/mm/yyyy hh:mm"):
=If(B1="";"";Now())
But it will change value everytime file opens.
You'll need save the value via macro.
You can use If function
Write in the cell where you want to input the date the following formula:
=IF(MODIFIED-CELLNUMBER<>"",IF(CELLNUMBER-WHERE-TO-INPUT-DATE="",NOW(),CELLNUMBER-WHERE-TO-INPUT-DATE),"")
Here is the solution that worked for me
=IF(H14<>"",NOW(),"")

Resources