I want MyMacro to run automatically when the cell value in A1 changes according to a vlookup formula. MyMacro copies a row then pastes it to a different section of the worksheet when a condition dependant on the value of A1 is met.
I have tried variations of Target.Address and the following, which have not worked:
Public Sub Worksheet_Calculate(ByVal Target As Range)
Static OldVal As Variant
If Range("A1").Value <> OldVal Then
OldVal = Range("A1").Value
If Not Intersect(Target(1), Range("C1401:I140")) Is Nothing Then
If Range("A1").Value < ActiveSheet.Range("A1").Value Then
Range("C140:I140").Value = Range("B55:H55").Value
ElseIf Range("B55").Value > ActiveSheet.Range("A1").Value Then
Range("C140:I140").Formula = ""
End If
End If
End If
End Sub
Public Sub MyMacro()
Worksheet_Calculate ([C140])
End Sub
With this, MyMacro only works when I manually hit Run Macro after each time A1 changes. How can I get this to be automatic?
Thanks.
If you want to do something when the value of your calculated Cell A1 changes then use the Worksheet_Change event on the relevant worksheet.
(Find the worksheet in the VBA editor and right-click to select "view code")
Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("$A$1"), Target) Is Nothing Then
... [call your change_routine]
end if
End Sub
Related
I have a Dynamic Table that changes based on the value of B1.
I want the value of B1 to change by clicking on another cell from another workbook. I'm super new to VBA and don't really know what I'm doing.
What I want is to click on any cell from column E and it will change the value of B1 to equal value of column O.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E2:E")) Is Nothing Then
Worksheets("Dynamic Table").Range("B1").Value = ActiveCell.Offest(0, 10)
End If
End If
End Sub
My Amateur Code
Do you mean sth like that
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 5 Then
If Target.Cells.CountLarge = 1 Then
Worksheets("Dynamic Table").Range("B1").Value = Target.Offset(, 10).Value
End If
End If
End Sub
I assumed you mean with another workbook just another worksheet.
I want an if clause to be true if the ActiveCell was "A1" before I clicked on "A2".
Its not connected with a button or anything. It has to work by just clicking on "A2".
That's my try i made:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(0, 0) = "A2" And ActiveCell.Cells(0, 0) = "A1" Then
MsgBox ("test")
End If
End Sub
Does someone have a solution?
I think you are using the wrong approach here.
If you use the SelectionChange event then it means the selected cell already changed. You have to stock the address of the last cell every time the event is called. To do the trick, put your condition before stocking the ActiveCell as the lastCellAddress and it works perfectly.
Here is the code with comments to help you understand:
'Declare the variable out of the event
Dim lastCellAddress As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Check if the last selected cell was A1 when selecting A2
If (ActiveCell.Address = "$A$2" And lastCellAddress = "$A$1") Then
'If the condition is true display your MsgBox
MsgBox ("test")
End If
'Set the current address as the last selected for next selection change
lastCellAddress = ActiveCell.Address
End Sub
I am trying to autofill text in a range(N6:N125) in excel 2013 via vba. My code works fine when range refers a single cell but not for a range. could you help me to find my mistake? My code is
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("N6:N125") = "" Then
Range("N6:N125").value = "My Text"
End If
End Sub
If you want to fill only the empty cells in N6 through N125 and some of the cells may already have values, but none have formulas, then a loop is not actually needed:
Sub marine()
Dim r As Range, s As String
Set r = Range("N6:N125")
s = "=IF(N6:N125="""",""MyText"",N6:N125)"
r.Value = Evaluate(s)
End Sub
If Range("N6:N125") = "" Then
This doesn't do the job. You will need to do this test cell-by-cell. An easier alternative would be this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Range("N6:N125").SpecialCells(xlCellTypeBlanks).value = "My Text"
End Sub
This will fill empty cells within the range. BUT, if the intent of your test is to check whether the whole range is empty, this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Application.CountA(Range("N6:N125")) = 0 Then Range("N6:N125").value = "My Text"
End Sub
You can use:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Selection
For Each cell In Selection
If cell = "" Then
cell.Value = "My Text"
End If
Next cell
End With
End Sub
This code will test each cell in your selection if empty and write "My Text" in it, but if it is not empty it will skip it.
In your code you are not testing each cell and you are not using a loop to move from one cell to another
I'm very new to VBA. I have a cell in the worksheet that live updates a value. the Value changes about once every month. Is there a way for me to record the day that the value changed in an adjacent cell?
For example,
If the value in A1 changes from 5 to 6 today, I just want to record today's date in A2.
I don't really need to keep a record of previous changes.
Thank you so much!
If you are using a Bloomberg function in cell A1 like BDP() or BDH() or BDS(), then you could use the Worksheet_Calculate() event macro to detect changes in that cell.
In this example, I use cell A3 as a "memory" to avoid re-posting the date too often:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
If [A1] <> [A3] Then
[A3] = Range("A1").Value
[A2] = Date
MsgBox "Date recorded"
End If
Application.EnableEvents = True
End Sub
The Worksheet_Change() is being fired when something on the sheet changes its value, so add something like this to your Sheet-Codemodule:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Debug.Print "A1 has been changed!"
'do something
End If
End Sub
Update:
It seems you need the calculate event also as you're using a formula. you could try something like this:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
ActiveSheet.Calculate
DoEvents
With Range("A1")
.Value = .Value
DoEvents
End With
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Range("A2").Value = Date
End If
End Sub
You could use the Worksheet_SelectionChange event. I guess it depends on what else you're doing in the worksheet as to whether it would fire or not.
You'd have to compare the value in A1 against what was in A1 previously which would be stored in another cell.
Hope someone can answer us.
We want to do the following:
When a value is entered in A1, this value is added to the value of B1.
We then want the value of A1 too reset to 0, but keep the value of B1
Is this possible in excel?
On the worksheet's VBA private module (right-click the report tab and hit "View Code"), enter the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = Range("a1").Address Then
Range("b1") = Range("b1") + Range("a1")
Range("a1").ClearContents
End If
Application.EnableEvents = True
End Sub
Worksheet_Change is called whenever a change is made to the worksheet.
Application.EnableEvents=False prevents the code from running continuously (without it, the change to B1 would also call Worksheet_change).
This code assumes that the value in B1 is always numeric (or blank). If it may contain character text, then a check will need to be put in place to either reset its value or not do the increment.
Using simoco's suggestion:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A1 As Range
Set A1 = Range("A1")
If Intersect(Target, A1) Is Nothing Then
Else
Application.EnableEvents = False
With A1
.Offset(0, 1) = .Offset(0, 1) + .Value
.ClearContents
End With
Application.EnableEvents = True
End If
End Sub