Excel cell as a user input and as a formula - excel

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

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

Clear cell content based on another cell for each row

How to clear a cell based on a change of other cell but for every row separately?
Say, I change AI5 and it clears AQ5, I change AI17 it clears AQ17?
I've got this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("AI5")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("AQ5").ClearContents
Application.EnableEvents = True
End Sub
And it works only for the cells in the code.
If I change both to a range of the column, it clears all column AQ, which is not what I need.
Thanks
Like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("AI" & Target.Row)) Is Nothing Then Exit Sub
Application.EnableEvents = False
Range("AQ" & Target.Row).ClearContents
Application.EnableEvents = True
End Sub
You need to tell excel which row, and the row will be the same as the Target.Row so you can use that.

Add a identifier if you change a cell value in a row

Is there a way in excel (VBA) to mark any change in a row (A2-A10) for example with an identifier if any cell in that row is changed. So for if A2 changes, add an X in A1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("A2:A10")) Is Nothing And Target <> "" Then '<- If there is any change in area A2:A10 and the value of the affect cell IS NOT empty then
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = "X"
ElseIf Not Intersect(Target, Range("A2:A10")) Is Nothing And Target = "" Then '<- If there is any change in area A2:A10 and the value of the affect cell IS empty then
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = ""
End If
Application.EnableEvents = True
End Sub
You can have a macro function to change the value or add new value when anything changes in the source cell.
This macro will be on the rows where the change needs to be recorded.

Check on ActiveCell before Target.Address

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

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