Stop NOW() Function from Auto Updating - vba

I have a cell which I want to record the time when adjacent cells to the left are changed. I do it with the NOW() function; however, the problem is that the time gets updated each time workbook is re-calculated. So, I am wondering whether there is any original way to prevent this very cell from auto-updating.
My current formula in the cell:
=IF(ISBLANK(H11),"",IF(H11="Interested",NOW(),IF(H11="Not Interested",NOW(),"")))
I personally have come up with this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Destination As Range
If Not Intersect(Target, Range("H:H")) Is Nothing Then
Target.Offset(0, 1).Value = Now
End If
End Sub
My issue with this code is that it is looking for any data in the cell. I am only wanting the cell to record the time when it contains either "Interested" or "Not Interested". The cell that I am looking at currently contains "In-progress". I have tried playing around with my code to try and incorporate these criteria's but I keep getting hit with errors. Any advice on what I can do to fix this? Thanks in advance.

Try the following code instead:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Destination As Range
If Not Intersect(Target, Range("H:H")) Is Nothing Then
If LCase(Trim(Target.Value2)) = "not interested" Or LCase(Trim(Target.Value)) = "interested" Then
Application.EnableEvents = False
Target.Offset(0, 1).Value = Now
Application.EnableEvents = True
End If
End If
End Sub

An alternative approach is a simple UDF that you use as =TimeChanged(H11)
Option Explicit
Option Compare Text
Public Function TimeChanged(theCell As Variant)
If TypeOf theCell Is Range Then theCell = theCell.Value2
If theCell = "Interested" Or theCell = "Not Interested" Then
TimeChanged = Now
Else
TimeChanged = ""
End If
End Function

Related

MsgBox SelectionChange define range

I have written the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myValue As Variant
If Range("B22") = "Yes" Then
myValue = InputBox("InsertInitialDetach")
Range("C22").Value = myValue
End If
End Sub
This code is meant to do the following thing: If I select Yes from a Yes/No drop down list, a message box appears and asks me to fill in a date.
The problem is that even after I fill the date, whatever I do afterwards, the box keeps on appearing and asking for the date. If I move two cells down, for example, the popup will continue to ask me for a date.
Please tell me what should I do to fix this error?
Would this be ok:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myValue As Variant
If (Not Intersect(Range("B22"), Target) Is Nothing) And (Range("B22") = "Yes") Then
myValue = InputBox("InsertInitialDetach")
Range("C22").Value = myValue
End If
End Sub
It checks every time whether you are changing Range("B22") and whether Range("B22") "Yes".
You are using the selectionChange event which triggers after any change in the area selected, if want to trigger on value changes use the change event
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myValue As Variant
On Error GoTo ErrorOccured
If Target.Address = "$B$1" And Target.Value = "Yes" Then
myValue = InputBox("Insert initialDetach")
Range("B2").Value = myValue
End If
ErrorOccured:
'Do when value is not valid
End Sub

VBA Pivot Table One Cell Reference Different Sheet

I'm using this current code to update a pivot table filter based on a cell value (E1) within the same sheet. What i would like to do is to update a filter based on a cell in a sheet named summary. If I set the filed in the current filed equal to the cell in the summary I need to press f2 and enter otherwise it won't work. I'm sure a little bit of tweaking and my code could work for it.
Any tips?
Private Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("E1")
If Target Is Nothing Then Exit Sub
On Error Resume Next
Application.EnableEvents = False
Sheets("Tech Pivot Table").PivotTables("PivotTable2").PivotCache.Refresh
With Me.PivotTables("PivotTable2")
.PivotCache.Refresh
.PivotFields("Name").CurrentPage = Target.Value
End With
Application.EnableEvents = True
End Sub
I think the problem is you're changing the value the Target variable up front when you should be checking to see if Target = "E1". Try the code below and let me know if it works.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("E1") Then
If Target Is Nothing Then Exit Sub
On Error Resume Next
Application.EnableEvents = False
Sheets("Tech Pivot Table").PivotTables("PivotTable2").PivotCache.Refresh
With Me.PivotTables("PivotTable2")
.PivotCache.Refresh
.PivotFields("Name").CurrentPage = Target.Value
End With
Application.EnableEvents = True
End If
End Sub

VBA define ranges and static date stamps

I need some help with this code as it doesn't work properly.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Target
With Cell
If .Column = Range("W:W").Column Then
Cells(.Row, "AC").Value = Int(Now)
End If
End With
Next Cell
End Sub
I am trying to get automatic static date stamps in column "AC" every time I fill in cells in column "W" and I want to start with row "19".
Tried to use
If .Column = Range("W19").End(xldown) Then
but it doesn't work.
I've just started using macro and vba and it will really help me if you can explain any solutions to me.
Thank you
Always turn off events if you are going to write to the worksheet in order that the Worksheet_Change event macro does not try to run on top of itself.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Columns("W:W")) Is Nothing Then
On Error GoTo bm_Safe_Exit
Application.EnableEvents = False
Dim rng As Range
For Each rng In Intersect(Target, Columns("W:W"))
If rng.Row > 18 Then _
rng.Offset(0, 6) = Date 'or Now for datetime
Next rng
End If
bm_Safe_Exit:
Application.EnableEvents = True
End Sub
This should handle multiple changes to column W like a block range paste.

Run-Time error '1004' with my VBA for hiding and unhiding rows

I have code for hiding and unhiding rows in my sheet based on changing the value in my dropdown. Every time I change the dropdown I get Run-Time error of '1004'. I had a private Sub before and changed it to a Sub but that doesn't seem to be the solution.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Target.Parent.Range("L6")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
Application.Run "dynamic_hide"
End Sub
Sub dynamic_hide()
If Target.Range = "$S$9:$S$51" Then
If Target.Range = 0 Then Rows("F9:T51").EntireRow.Hidden = True
If Target.Value <> 0 Then Rows("F9:T51").EntireRow.Hidden = False
End If
End Sub
You have a few problems going on here:
First, the default property of a Range object is Value, so Target.Range = "$S$9:$S$51" will always be false. Use Target.Address instead.
Second, don't use Application.Run to call Subs from the same VBProject. Use Call instead.
Third, you've not let the sub dynamic_hide know what Target is since Target is only a parameter of the Worksheet_Change event subroutine. You can solve this by declaring your sub like Sub dynamic_hide(ByVal Target As Range) And then you can use it: Call dynamic_hide(Target)
Lastly, since Target is a range you don't need to use Target.Range since Target is a range so you can simply omit every .Range from Target.Range Target.Parent.Range is fine.

Excel VBA - Issue with Worksheet_Change Code

I'm trying to create a macro that puts the date in a cell on another worksheet, when the initial worksheet is changed, but it gives me an out of range error. Is there any way to get around this, or am I simply unable to use the Worksheet_Change for this case. If so, what can I use? I was simply trying to test it, so I only have this so far:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Worksheets("Sheet4").Activate
Range("E1").End(xlDown).Offset(1, 0).Value = Date
Application.EnableEvents = True
End Sub
I now have this:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Worksheets("Testing Sheet").Range("E2").Value = "" Then
Worksheets("Testing Sheet").Range("E2").Value = Date
Else
' Worksheets("Testing Sheet").Range("E2").End(xlDown).Offset(1, 0).Value = Date
End If
Application.EnableEvents = True
End Sub
But the statement at the Else is giving me an error saying Application defined or object defined error. (side note I don't have it commented out in my actual code)
You will get error because you are checking condition this
If Worksheets("Testing Sheet").Range("E2").Value = "" Then
For Suppose you have some value E2 Cell. So it goes to Else statement
Worksheets("Testing Sheet").Range("E2").End(xlDown).Offset(1, 0).Value = Date
But probably don't have data below E2 Cell. So .End(xlDown) Selects E1048576 Cell Which is the last allowed row supported by excel.
.Offset(1, 0).Value tries to point E1048577 Cell which is not supported.
So you get Application Defined Error. Hope this makes sense.