How to reflect dates on specific cells corresponding to a list item chosen in Excel? - excel

Basically I have an excel sheet used for tracking statuses in hiring, where there are around 5 statuses (Briefing, Advertising, Shortlisting, Selection, Offering) and against each vacancy there's a status cell as a list of the above statuses and it's changed manually based on the status of the vacancy.
I have found a vba code (attached below) to reflect in a certain cell the date that the status has been changed, and if I choose another status (basically update the status) it overwrites the previous input, but what I need to do is a bit more complicated.
I created a column for each status and I need this code to reflect the date on each status's cell corresponding to the one I have chosen from the list, for example, choosing briefing for a specific vacancy will reflect the date on the corresponding cell in the "briefing" column, and if I change the status for that specific vacancy from the list to "selection", a date will reflect on the corresponding cell in the "selection" column.
The code that I've found so far is below:
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("N:N"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub

You can modify your code and add Select Case Statement to adjust column offset depending on data entry:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Long
Set WorkRng = Intersect(Application.ActiveSheet.Range("N:N"), Target)
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
Select Case Rng.Value2
Case "Briefing"
xOffsetColumn = 1 'Column O
Case "Advertising"
xOffsetColumn = 2 'Column P
Case "Shortlisting"
xOffsetColumn = 3 'Column Q
Case "Selection"
xOffsetColumn = 4 'Column R
Case "Offering"
xOffsetColumn = 5 'Column S
Case Else
xOffsetColumn = 6 'Column T - entry not listed above
End Select
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy"
Next
Application.EnableEvents = True
End If
End Sub

Related

How to apply a macro/vba formula to specific cells

I would like to apply vba formula to designated cells. The vba I am working on would be: If I type something on C2, a date stamp will be automatically put in D2.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 3 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
With Target.Offset(0, 1)
.Value = Now
.NumberFormat = "MM/DD/YYYY"
End With
End Sub
However, if I typed something on C1, a date stamp will appear as well. How can I limit the range of the vba? For example, I just want the date stamp from D2 to D5.
Thanks!!
You can apply Intersect function to check if target falls into the desired range, like this
Dim dr As Range
Set dr = Range("C2:C5")
If Not Intersect(target, dr) Is Nothing Then
... it is OK, go ahead
EndIf
The first answer is right, however here is the full version of it
Paste this in the Project of the table, not as a VBA module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("C2:C5"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each rng In WorkRng
If Not VBA.IsEmpty(rng.Value) Then
rng.Offset(0, xOffsetColumn).Value = Now
rng.Offset(0, xOffsetColumn).NumberFormat = "mm-dd-yyyy"
Else
rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub

Log date & time [...] in sheet2, when editing a cell in a specified range in sheet1

I have Sheet1 looking like this
running this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("B2:K10"), Target)
xOffsetColumn = 11
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each rng In WorkRng
If Not VBA.IsEmpty(rng.Value) Then
rng.Offset(0, xOffsetColumn).Value = Now
rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Else
rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub
What it does so far:
when I edit anything in range B2:K10 it adds the date & time on the right side of the table
What I need:
when I edit anything in the range specified above, log the date & time plus the corresponding step & item, all into Sheet2 starting from A2
Expected result:
And keep adding below last row over and over, never erase a record if a value is deleted from a cell in Sheet1. Will delete records manually if needed.
try this
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim rng As Range
Dim nextRow As Long
Set WorkRng = Intersect(Application.ActiveSheet.Range("B2:K10"), Target)
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each rng In WorkRng
If Not VBA.IsEmpty(rng.Value) Then
nextRow = Log.Cells(Rows.Count, 1).End(xlUp).Row + 1
Log.Cells(nextRow, 1) = Me.Cells(rng.Row, 1)
Log.Cells(nextRow, 2) = Me.Cells(1, rng.Column)
Log.Cells(nextRow, 3) = Now
Log.Cells(nextRow, 3).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
End If
Next
Application.EnableEvents = True
End If
End Sub
The code identifies the row number of the next empty row in Sheet 2, then writes the values into the first three cells of that row.
Edit: After seeing your edits to the code: You don't need to go through the hassle of declaring a worksheet variable and then setting it to the sheet. Instead, use the (Name) property to give the sheet a name, then you can access it from anywhere in the code via this name. I adjusted the code to reflect this.

How Does One Combine two Private Sub Worksheet_Change(ByVal Target As Range) in One Work Sheet?

Disclaimer: I'm very new to VBA, so obvious things probably fly over my head.
I'm trying to set up 2 columns that automatically update with that day's date when the column next to it receives new data in that sheet only.
I tried, with my limited knowledge, to create new variables, so that it has 2 'lines' to go with, if that makes sense, but it just stops working altogether.
Is there any way I can 'duplicate' the below code so that column O updates with today's date when column P is updated, and the same for column E when D is changed? If possible, it would be nice if column E didn't update when the text in D is "N/A"
Any help or pointers are appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("P:P"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
UpdateNextColumnIfNeeded Application.Intersect(Me.Range("P:P"), Target)
UpdateNextColumnIfNeeded Application.Intersect(Me.Range("D:D"), Target)
Application.EnableEvents = True
End Sub
Private Sub UpdateNextColumnIfNeeded(ByVal WorkRng As Range)
If WorkRng Is Nothing Then Exit Sub
Dim Rng As Range
For Each Rng In WorkRng.Cells
If IsEmpty(Rng.Value) Then
Rng.Offset(0, 1).ClearContents
Else
Rng.Offset(0, 1).Value = Now
End If
Next
End Sub
You will need to put checks in place so that you only have 1 worksheet_change, but allowing separate actions depending on the target range. Try something like:
Select Case Target.Column
Case 15
'that stuffs
Case 16
'that other stuffs
End Select
edtit1 yes, #GSerg, that is true... updated

How to target an specific columns after a delete without buttons

So i'm trying to make a code that deletes cells as long as they belong to the same column(horizontally) after deleting a cell from column "O".
Column N is all tick boxes linked to column FF. if they are set to true when the deletion happens, it should turn false.
Column O is where the deletion should happen( the objective is to simply press the cell and click delete or back space on the keyboard, not a button.)
Column P is sign date, which should also be deleted.
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("O:O"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
haven't been able to set the boxes to false. also i can't choose multiple cells, it only deletes one at a time.
This is the final result in which everything started working correctly.
Private Sub Worksheet_Change(ByVal Target As Range) '<----- Start of worksheet script
Dim WorkRng As Range '<----- selection
Dim Rng As Range '<----- selected cell(s)
Dim xOffsetColumn As Integer '<----- idk lol
Set WorkRng = Intersect(Application.ActiveSheet.Range("O:O"), Target) '<----- block selection for anything other than column O
xOffsetColumn = 1 '<----- Useless , but it means the cell one range forward will be changed.
If Not WorkRng Is Nothing Then '<----- if statement
Application.EnableEvents = False '<----- idk what this is for but ok.
For Each Rng In WorkRng '<----- Start of cell loop
If Not VBA.IsEmpty(Rng.Value) Then '<----- Second start of if statement
Rng.Offset(0, xOffsetColumn).Value = Now '<----- Date and time
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy" '<----- Date and time format
Else '<----- if's else statement.
Rng.Offset(0, xOffsetColumn).ClearContents '<----- Command for deletion.
Rng.Offset(, 147).Value = False '<----- Tick Box sets to false.
End If '<----- Second end of if statement
Next Rng '<----- end of loop
Application.EnableEvents = True '<----- I also don't know what this is for but ok.
End If '<----- First end of if statement.
End Sub

Excel VBA update the date in a cell if anything changes in a range of cells in the same row

I have a code where the date is updated in the cell in column D if any change was made in the cell in the same row in column F:
Private Sub Worksheet_Change(ByVal Target As Range)
' Code to put the date of the latest update following a change in the corresponding cell in column F
Dim WorkRng As Range
Dim rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("F:F"), Target)
xOffsetColumn = -2 'The date is put 2 columns to the left of column F
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each rng In WorkRng
If Not VBA.IsEmpty(rng.Value) Then
rng.Offset(0, xOffsetColumn).Value = Now
rng.Offset(0, xOffsetColumn).NumberFormat = "dd/mm/yyyy"
Else
rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub
Now I need to adapt this code so that the date is updated in the cell in column D if any change was made in the cells in the same row in columns F to K.
I have very little VBA knowledge and would be grateful for any help in adapting the code.
This appears to work:
Private Sub Worksheet_Change(ByVal Target As Range)
' Code to put the date of the latest update following a change in the corresponding cell in column F
Dim WorkRng As Range, roww As Long
Dim rng As Range
Set WorkRng = Intersect(Range("F:K"), Target)
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each rng In WorkRng
roww = rng.Row
If Not rng.Value = "" Then
Cells(roww, "D").Value = Now
Cells(roww, "D").NumberFormat = "dd/mm/yyyy"
Else
Cells(roww, "D").ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub
We just do not use OFFSET()

Resources