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.
Related
When I enter a value in Sheet 2 D1, the number should be entered in the table like if-statement and after that, the statement should disappear and the value should be still entered in the cell.
Table:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim OperationalArea As Range, AffectedArea As Range
Set OperationalArea = Me.Range("B2:G7")
Set AffectedArea = Intersect(Target, OperationalArea)
If Not AffectedArea Is Nothing Then
Application.EnableEvents = False
AffectedArea.Value = AffectedArea.Value
Application.EnableEvents = True
End If
End Sub
The event WorkSheet_Change() will not trigger if the change is caused only by recalculation.
You can use an event in Sheet2 instead:
' Event in Sheet2
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Me.Range("D1")) Is Nothing Then
' Change "Sheet1" to the name of the sheet with the table in B2:G7
ThisWorkbook.Worksheets("Sheet1").Range("B2:G7") = Me.Range("D1")
End If
End Sub
How do I render any cell in range ("A1:A10") uneditable (cannot be changed) if there is any content in 2 cells to the right (same row, column C), and make that cell editable again once the cell in same row column C becomes empty.
I tried the code below but it has no effect, i can still edit the cells in col A even with content in col C. Ideally I would like to have it done without protecting sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("C1:C10")) Is Nothing Then
If Target.Value <> "" Then
Target.Offset(0, -2).Cells.Locked = True
ActiveSheet.Protect Contents:=True
End If
Application.EnableEvents = True
End If
End Sub
Thank you,
Jay
Option Explicit
'*** Note: I need to reside in the Sheet Module for the
' sheet I am working on!
'*** Note: Make sure the Locked flags for A1:A10 and
' C1:C10 are cleared before implementing.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Application.EnableEvents = False 'Turn off Events to prevent loop!
If Not Intersect(Target, Range("C1:C10")) Is Nothing Then
ActiveSheet.Unprotect 'You need to unprotect before proceeding
For Each rng In Target
If rng <> "" Then 'Checking for Target.Address will always have a value!
rng.Offset(0, -2).Locked = True
Else
rng.Offset(0, -2).Locked = False
End If
Next rng
ActiveSheet.Protect Contents:=True 'Turn Protection back on
'*** If you have other protected elements DrawingObjects and/or Scenarios
' you need to include in line above.
End If
Application.EnableEvents = True 'Re-enable Events
End Sub 'Worksheet_Change
HTH
You are checking Target.Address on being an empty string. Why? Shouldn't that be Target.Value?
In top of this, what's going wrong? Did you debug the code, using breakpoints? What did you see? ... In case my answer does not satisfy your needs, please edit your question and add the needed information.
working on this macro on a dependent drop down menu
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M$10" Then
Range("O10").Value = "--select--"
End If
End Sub
I need to run this macro for all the cells in the column. It just work in the first cell
Can anyone help me please?
thanks!
You would need to use Application.Intersect in combination with .Offset() instead of the Target.Address method.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Application.Intersect(Target, Me.Range("B:B")) 'find all cells that were changed in column B
If AffectedRange Is Nothing Then Exit Sub 'exit if nothing in column B was changed
Application.EnableEvents = False 'make sure our value change doesn't trigger another Worksheet_Change event (endless loop)
On Error GoTo ENABLE_EVENTS 'make sure events get enabled even if an error occurs
Dim Cell As Range
For Each Cell In AffectedRange.Cells 'loop through all changed cells in column B
Cell.Offset(ColumnOffset:=1).Value = "" 'move from B one column to the right and reset value
Next Cell
ENABLE_EVENTS: 'in case of error enable events and report the error
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub
This will observe column B and delete the value in C whenever a cell in B was changed.
I'm working with the Worksheet_Change(ByVal Target As Range) event.
I wanted to run a macro if either on of cells A1 or A2 are changed.
So the target range is set to [A1, A2]
Then, if A1 is changed, I want to clear A2, or the other way around (A2 is changed: clear A1).
Now the problem:
If A1 value is changed. the macro clears A2. That's seen as a change in A2, so it clears A1, which is a change, etc...
I'm sure it's something simple, but I can't see how I can have cell A1 be changed without triggering the change macro if A2 is being cleared.
Anyone who has experience in this?
You can accomplish this by toggling events off and on, like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1:A2")) Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
If Target.Address = "$A$1" Then
Me.Range("A2").Clear
ElseIf Target.Address = "$A$2" Then
Me.Range("A1").Clear
End If
SafeExit:
Application.EnableEvents = True
End Sub
There are a few edge cases here, around changing more than one cell eg via copy pasting a range (possibly include other cells too).
Here's a version thats more robust in those cases
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Me.Range("A1:A2"))
If rng Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
Select Case rng.Address(0, 0)
' If both changed, what now? Default to clearing A2
Case "A1", "A1:A2"
If Not IsEmpty(Me.Cells(1, 1)) Then
Me.Cells(2, 1).Clear
End If
Case "A2"
If Not IsEmpty(Me.Cells(2, 1)) Then
Me.Cells(1, 1).Clear
End If
End Select
SafeExit:
Application.EnableEvents = True
End Sub
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