Check on ActiveCell before Target.Address - excel

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

Related

Cell equal value of active cell which is a drop down

I have a cell which i declared as active cell using developers tool in excel
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("C1").Value = ActiveCell.Value
End Sub
this cell is equals to the value of a drop down cell i created from data validation.
My Problem is cell C1 is not changing when i select a new value from the drop down
Here you go. When B1 is changed C1 is assigned the value of B1. It doesn't matter how the change was carried out, from the keyboard or by selection from a drop-down.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("B1").Address Then
' prevent the coming change from causing a call of this procedure
Application.EnableEvents = False
Cells(1, "C").Value = Target.Value
Application.EnableEvents = True
End If
End Sub

Automatically run macro when a value changes as the result of Vlookup

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

Excel cell as a user input and as a formula

I am trying to make cell E23 as a user input and as a formula cell if user does not enter any value. For eg: If user enters a value in cell E23, then consider that value. If user does not enter any value then copy value from cell B23. Below is the vba code which I tried.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$23" And Target.Value = "" Then
Target.Formula = "=B23"
End If
The code works fine until I change the value in cell B23 by a selection made in the dropdown combobox. When I change the selection in combobox from option 1 to option 2, new value gets updated in cell B23 which must be copied into E23. But it gives me a runtime error '13' Type mismatch.
Any help is appreciated. Thank you
The issue here is that this line
Target.Formula = "=B23"
changes the target cell, and that triggers a Worksheet_Change event that changes the target cell, and that triggers a Worksheet_Change event … and so on.
So you need to disable the events before you change the target cell (and re-enable them afterwards).
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$23" And Target.Value = vbNullString Then
Application.EnableEvents = False
Target.Formula = "=B23"
Application.EnableEvents = True
End If
End Sub
Alternative that works also when multiple cells are selected:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E23")) Is Nothing Then
Application.EnableEvents = False
If Range("E23").Value = vbNullString Then
Range("E23").Formula = "=B23"
Else
Range("E23").Value = Range("E23").Value - Range("E17").Value
End If
Application.EnableEvents = True
End If
End Sub

Excel VBA Click Cell to Fire Macro - Not working with Merged Cells

I am using the code below to fire a macro on the click of a cell. The cell in question is a header "Mitch's Macro" but it on merged cells B5 through J5. I have tried naming this merged range to MITCH, but it still doesnt run on click... Any ideas? Thank you in advance!
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("MITCH")) Is Nothing Then
MsgBox ("Hello World")
End If
End If
End Sub
The problem is Selection.Count = 1.
The merged cells have more than one cells so once you select any cell in the merged area, the code doesn't get executed.
Please give this a try...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("MITCH")) Is Nothing Then
MsgBox ("Hello World")
End If
End Sub
Edit:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Set rng = Range("MITCH")
If Target.CountLarge > rng.Cells.Count Then Exit Sub
If Not Intersect(Target, rng) Is Nothing And Target.Cells(1).Address = rng.Cells(1).Address Then
MsgBox ("Hello World")
End If
End Sub
After a little more thinking I realised that most answers have a few drawbacks, and I think this is what we're really after:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Set rng = Range("MITCH")
If Target.Address = rng.MergeArea.Address Then 'check if what's selected matches the named range entirely
MsgBox ("Hello World")
End If
End Sub
As this checks whether the cells you have selected perfectly map onto the named area - specifically the MergeArea of the named range.
Matching with Intersect just checks if the selection contains the named Range
Matching by TL cell means any selection with the same TL as the named Range will also return a positive. E.g, if [B2:D3] is your merged named range, then matching by [B2] will return positive if [B2:D3] is selected (as expected), but also when [B2:XX100] is selected
This code only returns positive when the areas are identical, i.e. only the merged cell is selected.
If you have named the range you could use the code below
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tName As String
On Error Resume Next
tName = Target.Name.Name
On Error GoTo 0
If Len(tName) > 0 Then
If tName = "MITCH" Then
MsgBox ("Hello World")
End If
End If
End Sub

Add value from one cell to another then reset cell value. Excel

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

Resources