I've already found article about how to automatically add date on a cell. I used it and change it to add time on a cell (Code below).
Dim rng1 As Range
Set rng1 = Intersect(Range("D:D"), Target)
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
rng1.Offset(0, -2).Value = Time()
Application.EnableEvents = True
End Sub
Question is:
How can I change code to do the same in the same sheet but in different column?
Now I entering data on column "D" and get time on column "B". I want also enter data in column "K" and get time on column "I"
Unfortunately I'm not good in coding and I don't have any idea how correctly edit this code.
I also forgot to tell that I want to use this macro on excel placed on Sharepoint website.
Can I ask you guys for help??
Best regards Christof
Since the offset D to B and K to I is -2 in both cases you can use
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 Or Target.Column = 11 Then ' D or K
Application.EnableEvents = False
Target.Offset(0, -2).Value = Time()
Application.EnableEvents = True
End If
End Sub
Related
This is a simple answer but one I cannot find.
I have two columns of data.
Column A (1) has yes/no data validation list options in every cell.
Column B also has data validation with say 6 strings of text options in every cell.
But I only want each the corresponding cell (column B) to update in the same row as column A
e.g A20 toggled, then B20 is updated. Like so
A20 is selected “Yes” from the dropdown option and B20 is updated with the string “complete” which is one of the states you can select in the dropdown boxes manually in every cell in column B.
I had some code but I would have to write an argument for every cell and then two macros for every yes / no.
This is code that works for one cell only but this is not ideal for many cells but it works
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A20")) Is Nothing Then
Select Case Range("A20")
Case "Yes": Macro_001
Case "No": Macro_002
End Select
End If
End Sub
Sub Macro_001()
Application.Calculation = xlManual
Application.ScreenUpdating = False
Sheets("August 2020").Select
Sheets("August 2020").Range("B20").Select
ActiveCell.FormulaR1C1 = "Complete"
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
Sub Macro_002()
Application.Calculation = xlManual
Application.ScreenUpdating = False
Sheets("August 2020").Select
Sheets("August 2020").Range("B20").Select
ActiveCell.FormulaR1C1 = ""
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
There much be an easier way with .range perhaps
Thanks in advance
In the developer tab click view code, choose the sheet you want the macro to run on, make sure the upper left drop down says worksheet and the upper right says Change (I'll assume your sheet has headers):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim ChangeCell As Range
Dim numrows As Long
numrows = Cells(Rows.Count, 1).End(xlUp).Row
Set TriggerCells = Range("A1")
Set KeyCells = Range("B2:B" & numrows)
If Application.Intersect(TriggerCells, Range("A1")) = "[Column Header Text]" Then
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
If Target.Value = "Yes" Then
Range("B" & Target.Row).Value = "Completed"
End If
End If
End If
End Sub
Try that, see if it works for you.
I need to create an Excel document with updating totals in the A column, based on numbers entered in the B column. Each respective cell in row A should update based on its equivalent B cell value whenever a new value is added, and then the value entered into B is cleared once added to A.
I have gotten things working for one single row but don't have knowledge or understanding on how to best make this work for EACH cell pair in the entire column. I really don't want to copy and paste this 10,000 times and update the cells to reference the correct pair. Code for single cell:
Private bIgnoreEvent As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If bIgnoreEvent Then Exit Sub
bIgnoreEvent = True
Cells(1, 2) = Cells(1, 2) + Cells(1, 1)
Cells(1, 1) = ""
bIgnoreEvent = False
End Sub
I am hoping this can be achieved with a loop function, or a range of some sort.
This should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
'any updates in ColB?
Set rng = Application.Intersect(Target, Me.Columns(2))
If Not rng Is Nothing Then
Application.EnableEvents = False '<< prevent re-triggering of event
'Process each updated cell
For Each c In rng
If IsNumeric(c.Value) Then
With c.Offset(0, -1)
.Value = .Value + c.Value
End With
c.ClearContents
End If
Next c
Application.EnableEvents = True '<< re-enable event
End If
End Sub
I'm creating a report-styled sheet in Excel, and trying to get a timestamp to automatically be entered in cell "P4" if cell "I6" has a value of "Completed"
I've tried using =IF formulas, which worked, but I'm unable to toggle iterative calculation on the machines this sheet will be working on.
I'm fairly new to writing my own VBA, and I'm having some trouble getting my current code to work. Below is what I currently have, which isn't giving me any results.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As String
Set r = Cells("I6")
If r.Value Is Nothing Then Exit Sub
If r.Value <> "Completed" Then Exit Sub
If r.Offset(-2, 7).Value <> "" Then Exit Sub
Application.EnableEvents = False
r.Offset(-2, 7) = Now()
Application.EnableEvents = True
End If
End Sub
I expect the code to give me a current timestamp in Cell "P4" once the value "Completed" is entered into cell "I6", but nothing is showing up. How would I correct it in order to get the value based timestamps?
As this sub is called at every cell's change (and you may use it later for other cell-checks also), check by Intersect first, if "your" cell is affected.
The changed range is given as Target (which may be a single cell or a complete range, e. g. when you paste on it). If that is intersected with your monitored cell I6, you can go ...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RelevantArea As Range
Set RelevantArea = Intersect(Target, Me.Range("I6"))
If Not RelevantArea Is Nothing Then
If Target.Value = "Completed" Then
Application.EnableEvents = False
Me.Range("P4").Value = Now()
Application.EnableEvents = True
End If
End If
End Sub
I would like to determine if a cell in my range contains a date (any date) and if it does to exit the sub with a message.
The date format looks like this: dd-mmm-yy but is generated by a formula within the cell.
Here is some of my code already written along with some pseudo code of what I want to achieve.
Sub RemoveRowButton()
'This Macro deletes a row where the button is clicked.
'Variables
Dim row As Long
Dim varResponse As Variant
Application.ScreenUpdating = False
'Message box confirming user is doing the right thing
varResponse = MsgBox("Delete this row? 'Yes' or 'No'", vbYesNo, "Delete Row")
If varResponse <> vbYes Then Exit Sub
'Carry on with deleting row.....
Set rng = ActiveSheet.Buttons(Application.Caller).TopLeftCell.EntireRow
*******Pseudo Code *******
'Check if the row to be deleted has a date in the D Column of the range (which is a Row)
'If IsDate **in D column of the Range is ture*** Then
'MsgBox "This Row Contains a Date!"
'End If
'Unprotect sheet
ActiveSheet.Unprotect Password:="***"
'Delete row on button row
rng.Delete
'Protect sheet again
ActiveSheet.Protect Password:="***"
End Sub
If you could explain your code/answers too I would be grateful, Thanks.
EDIT:
Thank you for all the help, I have, through trial and error created this which works for me.
Set rng2 = rng.Cells(, 4)
If IsDate(rng2.Value) Then 'Check Cell for Date
MsgBox "Warning: This Row Cannot be deleted!"
Exit Sub
End If
Since I am unfamiliar with VBA I do not know if this is "OKAY" in the sense of best practices. If not and you feel like correcting it please do so.
Here is an idea for your problem. Install the code in the code sheet of the worksheet on which you wish to have the action (not in a standard module like 'Module1' !!) Note that the code reacts to a double-click in column D from row 2 down to the last used row in column A. You can adjust that. Follow the directions in the code itself. I use this method instead of the button you seem to have in every row of your sheet - a matter of preference, but used here for demonstration and to avoid creating buttons.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
' 03 Jan 2019
Dim Rng As Range
Dim R As Long
Dim Cell As Range
Dim i As Integer
R = Cells(Rows.Count, "A").End(xlUp).Row ' last used row in column A
Set Rng = Range(Cells(2, 4), Cells(R, 4)) ' used range in column D
If Not Application.Intersect(Target, Rng) Is Nothing Then
' if a cell in Rng as double-clicked:-
R = Target.Row
Set Rng = Range(Cells(R, "A"), Cells(R, "S"))
For Each Cell In Rng
With Cell
If IsDate(.Value) Then
For i = 3 To 1 Step -1
' check if the Numberformat contains all of "m", "d" and "y"
If InStr(1, .NumberFormat, Mid("dmy", i, 1), vbTextCompare) = 0 Then Exit For
Next i
If i = 0 Then ' all 3 were found
If MsgBox("Do you want to delete row " & R & " ?", _
vbQuestion Or vbYesNo, _
"Click ""No"" to keep the row") = vbYes Then
Rows(R).Delete
End If
Exit For
End If
End If
End With
Next Cell
Cancel = True ' ends in-cell editing
End If
End Sub
The code carries out two checks on each cell (A:S). It first checks if its value is a date. Then, presuming it is a number, it checks the cell format. If the NumberFormat includes all of the letters 'm", "d" and "y" it is confirmed as a date and released for deletion before which the user can confirm his intention.
This method may require a little fine tuning. Firstly, if the cell has text date a different second check would have to be carried out. Second, if the date format consists of only 2 of the 3 criteria the test for their presence in the mask must be reduced accordingly. Either of these modifications, or both, could be implemented once the nature of your data is better understood.
#J4C3N-14 did you try:
Sub Test_Date()
Dim strDate As String
With ThisWorkbook.Worksheets("Sheet1")
strDate = .Range("A1").Value
If IsDate(strDate) Then
'Code
End If
End With
End Sub
I have a spread sheet which is used for basic scheduling of tasks.
Dates for the calendar run along Row 1 from O-NO and everything below is job related including due dates.
I am trying to automatically add a note to the calendar section of the sheet when a date is added to column E. The word “Due Date” is update to the corresponding text row/date column.
Colum E = due dates, Columns O to NO (rows are infinite) are days Jan – Dec. I have created the cell formula =IF((AND($E452=$1:$1)),"Due Date","") which is cell specific.
I need to keep the cells clear of formulas because they are used for adding other details so a Macro is the way to go.
I thought I could convert the formula to a macro and then manipulate the code to do what I need across all of the calendar cells. Below is the result.
Sub DueDate()
'
' DueDate Macro
'
'
Range("IM451").Select
ActiveCell.FormulaR1C1 = "=IF((AND(RC5=R1)),""Due Date"","""")"
Range("IM452").Select
End Sub
Firstly I tried a number of ways just to get this macro to run automatically without having to manually run it. For some reason I couldn’t get it to work.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
MACRO CODE HERE
End If
End Sub
Plus a couple of other versions
Second step was to get it to work across all of the calender cells, another fail.
Would really appreciate some assistance on this.
Thank you
CRB
If I understand what you're trying to do correctly, then try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Application.EnableEvents = False
If Target.Cells.Count = 1 And Target.Column = 5 Then
Set c = Range(Cells(Target.Row, "N"), Cells(Target.Row, "NO")).Find("Due Date")
If Not c Is Nothing Then c.ClearContents
Set c = Nothing
If IsDate(Target.Value) Then
Set c = Range("N1:NO1").Find(Target.Value)
If Not c Is Nothing Then Cells(Target.Row, c.Column).Value = "Due Date"
End If
End If
Application.EnableEvents = True
End Sub
When a date is entered in column E, this will look for that date in range N1:NO1, and if the date is found, will insert "Due Date" in the matching column of the target row.