I have different set of values in cells G3:G4 and D15:D10000 in a single sheet. I want run two separate codes when G columns or D columns are changed. How I can identify which set of columns are changed?
out this in your worksheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G3:G4")) Is Nothing Then
'code when some cell in range "G3:G4" is changed
ElseIf Not Intersect(Target, Range("D15:D10000")) Is Nothing Then
'code when some cell in range "D15:D10000" is changed
End If
End Sub
Put the code below in your relevant worksheet, in the Worksheet_Change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
Set WatchRange = Application.Union(Range("G3:G4"), Range("D15:D10000"))
Set IntersectRange = Intersect(Target, WatchRange)
If Not IntersectRange Is Nothing Then
Select Case Target.Column
Case 4 ' column D
Call A
Case 7 ' column G
Call B
End Select
End If
End Sub
Below are examples of Sub A and Sub B:
Sub A()
MsgBox "Running Sub A"
End Sub
Sub B()
MsgBox "Running Sub B"
End Sub
Related
I'm trying to make a Spin Button that will edit the active cell, but only when it is within a specific cell range. I want the code to start up if the active cell is within the range of Sheet 1 cells J63:J97 and, not run if it is outside that range.
This is the code I have so far. It will edit the active cell, as needed. However, it is not limited to the range I need it to be.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
SpinButton1.Value = Selection.Value
End Sub
Private Sub SpinButton1_Change()
Selection.Value = SpinButton1.Value
End Sub
Try using Application.Intersect.
I have defined a separate Function to do the job.
This code is tested and it works:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If checkIntersection(Target, Range("J63:J97")) Then
SpinButton1.Value = Selection.Value
End If
End Sub
Private Sub SpinButton1_Change()
If checkIntersection(Selection, Range("J63:J97")) Then
Selection.Value = SpinButton1.Value
End If
End Sub
'Check if Range1 and Range2 are intersecting
Function checkIntersection(range1 As Range, range2 As Range) As Boolean
checkIntersection = Not Application.Intersect(range1, range2) Is Nothing
End Function
may check selection with Intersect
Private Sub SpinButton1_Change()
If Not Intersect(Selection, Range("J63:J97")) Is Nothing Then
Selection.Value = SpinButton1.Value
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("J63:J97")) Is Nothing Then
SpinButton1.Value = Selection.Value
End If
End Sub
I have an Excel workbook with multiple worksheets. I have a cell in WORKSHEET A with range name TRACK1 and a cell in WORKSHEET B with range name TRACK2.
Each TRACK1 and TRACK2 are validated from a list. The user can change either cell from the drop-down list shown when the cell is selected.
I want to be able to allow the user to change either and have the other be also changed to match. Change value of TRACK1 and TRACK2 is changed, and vice versa.
I know how to do this basic macro, but how to stop the event propagating?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("TRACK1")) Is Nothing Then
Range("TRACK2") = Range("TRACK1")
End If
If Not Application.Intersect(Target, Range("TRACK2")) Is Nothing Then
Range("TRACK1") = Range("TRACK2")
End If
End Sub
In worksheet A's code module, use:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("TRACK1")) Is Nothing Then
Worksheets("WORKSHEET B").Range("TRACK2") = Range("TRACK1")
End If
Application.EnableEvents = True
End Sub
In worksheet B's code module, use:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("TRACK2")) Is Nothing Then
Worksheets("WORKSHEET A").Range("TRACK1") = Range("TRACK2")
End If
Application.EnableEvents = True
End Sub
I am new to excel macro.
What I need to to is by clicking on specific cell in A column I will know It`s value. Can you help me with this?
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D1:D200")) Is Nothing Then
MsgBox Range.("D" & Row_No).Value
End If
End If
End Sub
Use the code below to check the value of any cell in Column A once you click on it, this code should be added to your relevant Sheet to Worksheet_SelectionChange event :
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
' setting Column A as watched Range
Set WatchRange = Range("A:A")
Set IntersectRange = Intersect(Target, WatchRange)
If Not IntersectRange Is Nothing Then
If Selection.Count = 1 Then
MsgBox Target.Value
Else
MsgBox "You have selected more than 1 cell !"
End If
End If
End Sub
I have a Sub that I would like to run whenever cells are updated to contain a certain value.
Right now I'm using code like the following:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count = 1 Then
If Target.Value = XYZ Then
my_sub a, b, c
End If
End If
End Sub
The issue right now is that the macro only fires when I edit these cells directly, not when changes in other cells force these cells to change.
Additionally, these cells are not well defined, so I can not hard code "when A5 changes", for example. I need this to fire every time any cell in my workbook is updated (manually or through formulas) to meet my condition.
Provided your target is only a single cell with a formula that needs to be monitored, this will work:
Option Explicit
Dim tarVal As Variant
Private Sub Worksheet_Activate()
tarVal = ActiveSheet.Range("A1").Value ' change range parameter to the address of the target formula
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tempVal As Variant
tempVal = ActiveSheet.Range("A1").Value
If tempVal <> tarVal Then
tarVal = tempVal
' your code here
MsgBox "The value of A1 has changed" ' for testing purposes only, delete later
End If
End Sub
Edit
The following code works for an entire range of cells, but only if automatic calculation is turned on. In case the monitored cells are non-contiguous, just use union statements when defining the target range. (The target range is A1:A10 in this example). This is under the assumption that only one of formulas in the target range can change its value at a time. If multiple target formulas can do that, then remove Exit for in the Worksheet_Change subroutine.
Option Explicit
Dim tarCellCount As Long
Dim tarRng As Range
Dim tarVals As Variant
Private Sub Worksheet_Activate()
Dim i As Long
Dim cll As Range
Set tarRng = ActiveSheet.Range("A1:A10") ' change range parameter to the addresses of the target formulas
tarCellCount = tarRng.Cells.count
ReDim tarVals(1 To tarCellCount) As Variant
For Each cll In tarRng
i = i + 1
tarVals(i) = cll.Value
Next cll
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changeBool As Boolean
Dim i As Long
Dim cll As Range
Dim tempVal As Variant
For Each cll In tarRng
tempVal = cll.Value
i = i + 1
If tempVal <> tarVals(i) Then
tarVals(i) = tempVal
changeBool = True
Exit For
End If
Next cll
If changeBool Then
' your code here
MsgBox "The value of one of the cells in the target range has changed" ' for testing purposes only, delete later
End If
End Sub
Add your cells to be tracked to a named formula (named range). I used rngValue
Use a static variable to track how many times the value you want to track occurs in this range
Use the Calculate event to check if the number of occurences changes
code
Private Sub Worksheet_Calculate()
Dim StrIn As String
Static lngCnt As Long
Dim lngCnt2 As Long
StrIn = "apples"
lngCnt2 = Application.WorksheetFunction.CountIf(Range("rngValue"), StrIn)
If lngCnt2 <> lngCnt Then
lngCnt = lngCnt2
Call mysub
End If
End Sub
Target is a range that CAN contain more cells. This code should work for you.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
For Each cell In Target.Cells
If cell.Value = XYZ Then
my_sub a, b, c
End If
Next cell
End Sub
Edit: I see that you want to fire that also when formula is updated defined value. It can be slow if you will check every cell, but really depends on the size of your file. Here is some code to give you idea how to do it.
Private Sub Workbook_SheetCalculate(ByVal sh As Object)
For Each cell In sh.Cells.SpecialCells(xlCellTypeFormulas).Cells
If cell.Value = XYZ Then
my_sub a, b, c
End If
Next cell
End Sub
I know how to check to see if a column has changed, like so
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H5")) Is Nothing Then
'Pop up a message saying H5 has changed
End If
End Sub
If I want to change another column on the same row, I can do this
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H5")) Is Nothing Then
Range("A5").Value = "Look at me!"
End If
End Sub
Now what if I want to achieve the above, but for all columns in the range of row1 to the end of the table? Something like this (note, I know this wouldn't work)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H1:H")) Is Nothing Then
Range("A" & Target.row).Value = "Look at me!"
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
Range("A" & Target.row).Value = "Look at me!"
End If
End Sub
Though you should note that Target can be a multi-cell range if more than one cell is updated in the same operation.
This may be safer:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c in Target.Cells
If Not Intersect(c, Range("H:H")) Is Nothing Then
'3 ways to accomplish the update
Range("A" & c.Row).Value = "Look at me!"
c.EntireRow.Cells(1).Value = "Look at me!"
Cells(c.Row, 1).Value = "Look at me!"
End If
Next c
End Sub