I am using the following code to insert todays date into a cell when a user clicks onto another cell in that row.
At the moment the code is set out like so; if a user clicks cell AQ8 then insert date into cell AS8.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Address = "$AQ$8" Then
Range("AS8").Value = DATE
End If
End Sub
however, now i want to change this code slightly so that when a user changes any AQ cell in either row then the subsequent AS cell has the date inserted into the appropriate cell for that row. So if cell AQ9 is clicked then AS9 shows todays date and when they click AQ10 then AS10 shows todays date and so on. Would this be written as a target row function? and if so how would i write something like this? Can someone please show me how i could get this to do what i need, thanks in advance
If you don't want to refer to columns by number, i.e. you want to use "AQ" instead of 43, then the following should work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = Range("AQ1").Column Then
Range("AS" & Target.Row).Value = Date
End If
End Sub
The reference to row 1 in AQ1 is a dummy, since the code just selects the .Column of that range.
I'm using Worksheet_Change because you said "when a user changes any AQ cell". If you only want this to happen when a user clicks on a cell, then you can keep using the Worksheet_SelectionChange event.
Update like that:
If Target.Column = 43 Then
Range("AS" & Target.Row).Value = Date
End If
every rows in column AQ = 43 are updated with the date.
Related
I have an excel sheet that updates cell 'B' with the price of bitcoin from the web (Powerquery) at intervals. The idea is that when the cell updates with a new price, I am supposed to take the new price and store it in another cell, 'G'. Then, wait for the price change again in cell 'B' to store it in a new blank cell. I have made a worksheet_change code that detects cell 'B' changes and stores the change in another column. But this only works when cell 'A' is changed manually, not when updated from the web. How can I make it work even for updates from the web? Code is as below:
`Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("B2").Address Then
Call Update_Price
End If
End Sub
Private Sub Update_Price()
Range("G1").End(xlDown).Offset(1).Select
ActiveCell = Cells(2, 2)
End Sub`
I'm trying to add a formula to a specific cell that says "Enter user data then add 20" Ex: If a user enters 10 into a cell when they press enter the cell returns 30.
I've tried a formula that says, =this cell (plus this cell plus 20) but can't have a formula and user input in the same cell.
Create a named range on the cell that you are wanting to add 10 to, I called that range rngCellToAdd10To ...
Then add the below code (using the VBA editor) to the worksheet object that the cell you want to monitor changes for ...
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Intersect(Target, Range("rngCellToAdd10To")) Then
Application.EnableEvents = False
Range("rngCellToAdd10To").Value = Range("rngCellToAdd10To").Value + 10
Application.EnableEvents = True
End If
End Sub
... then add a number to the cell and watch it go.
If you're not sure how to add code into the VBA editor, I suggest Googling it.
I am new to VBA and am still learning. What I am trying to accomplish seemed easy enough but am having difficulty. I need to write a macro that auto hides a row when a date (any date) is entered into the cell of a specific column.
E.g. data is recorded in columns A:F. When a "shipped" date is entered into G2, row 2 auto hides.
Any help would be greatly appreciated.
Thanks.
McJ
The following VBA code will hide the associated row if a cell in column 'G' is edited and changed to a date (number) between zero and the current date:
Option Explicit
Private lastSelectedCell As Range
Private Sub Worksheet_Activate()
Set lastSelectedCell = Range("A1")
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' If the most recently selected cell was in column 6 ('G')
If (lastSelectedCell.Column = 6) Then
' If the previously selected cell was a date on or before today
If ((lastSelectedCell.Value <= Date) And (lastSelectedCell.Value > 0)) Then
' Hide the entire row
Rows(lastSelectedCell.Row).EntireRow.Hidden = True
End If
End If
Set lastSelectedCell = Target
End 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 was trying to make an excel cell print today's date like this
=DATE(2013,3,23)
But it only prints that date until some user changes the numbers.
So please help me!
=Today()
This formula is dymanic and will always show the current date.
You could do this, or use the Date() formula, and then copy/paste values.
But even if you do this, the user can still "change the numbers" after the fact, so you can't avoid that unless you protect the sheet, or implement some sort of event-based macro to maintain the desired date.
Here is a simple macro that will always put today's date in Cell A1 no matter what the user does to it. Place this in the Worksheet's code module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub 'This will only trigger the event if the cell A1 changes. You can modify this as needed.
Target.Value = DateSerial(Year(Now()), Month(Now()), Day(Now()))
End Sub