Automatically execute a macrop - vba

I want to write a VBA macro which triggers another macro after changing the value of a cell. This is what I came up with.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("n7")) Is Nothing Then Macro1
End Sub
However it doesn't work.

Try the code below, this code needs to be in the worksheet you are trying to modify cell "N7" and then call Macro1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("N7")) Is Nothing Then Call Macro1
End Sub
Example of Sub Macro1, located in another code module:
Sub Macro1()
MsgBox "Hello"
End Sub

Related

Check whether a changed cell in a named range excel vba

I just need to monitor changes in a worksheet, not in the whole worksheet, just inside a named range. So if any kind of change occurs in any cell it should check the change occurs in that named range and if so some function has to do.
Here is my code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ir As Boolean
ir = Application.Intersect(Target.Address, Range("bd_main"))
If ir = True Then
MsgBox "change"
End If
End Sub
But it triggers an error saying Type Mismatch in the Application.Intersect function for Target.Address
What I did wrong there which causes such error.
With slight modification, use the code below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("bd_main")) Is Nothing Then
MsgBox "change"
End If
End Sub

How to trigger Worksheet_Change from OLEOjects?

I want the macro to run automatically when the combo box value in worksheet change but I did not find any code that can fix my code.
Here is the code
Private Sub Worksheet_Change(ByVal Target As Range)
With Me.CBbox
If Not Intersect(Target, ActiveSheet.Range(OLEObjects.LinkedCell)) Is Nothing Then
Call Macro
End If
End With
End sub

Macro doesnt appear on the List after copy-pasting code

I am completely new to VBA and hence have no idea what I'm doing...
Below is the description of my problem and also the code in question.
What I originally wanted to do was this:
Copy a clicked cell, Select new sheet, Select any cell, Paste as values
Now i found a code that apparently does the trick which is this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing Then
Cancel = True
If Target.Row > 1 And Len(Target.Value) Then Worksheets("S11").Range("C2").Value = Target.Value
End If
End Sub
I say "apparently" as I am not able to test it. Here is the problem:
I saved the workbook as excel macro-enabled workbook
Clicked ALT Q - to go back to my workbook
I then ALT F8 to run it - but there is nothing there... blank....
what am I missing?
In a module, have the macro you wish to use.
public sub mymacro(r as excel.range)
If Not Intersect(r, Columns("A")) Is Nothing Then
If r.Row > 1 And Len(r.Value) Then Worksheets("S11").Range("C2").Value= r.Value
End If
end sub
public sub wbtest()
mymacro activecell
end sub
and use like so
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing then
mymacro target
end if
end sub

Sheet specific macro will not run when condition is met

I have a very simple workbook macro for testing and it is not executing when the condition is met. Do you know why? Macros are enabled and modules are working, however, sheet codes will not work. Any idea why?
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$J$3" Then
Call macro1
End If
End Sub
And then I tried
if A1 = "correct!"
msgbox "hey"
else <do nothing>
If I adjust anything in cell J3 or putting "correct!" in A1 in that sheet, neither code will execute. macro1's code is simply msgbox "Hey". Any idea what I can do?
Thanks!
I used Workbook_SheetChange and it worked for me:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Sheet1" And Target.Address = "$J$3" Then
Call macro1
End If
End Sub
Sub macro1()
MsgBox "hello world!"
End Sub
I hope I'm understanding your post correctly, you want that once you modify a value in your worksheet at Cell J3, and the value in Cell A1 (at the same worksheet) is "correct!", that a MsgBox with "hey" will pop-up.
The code in your Worksheet_Change event is good, just modify the code in macro1 (which can be placed at another code module).
Sub macro1()
If Range("A1").Value = "correct!" Then
MsgBox "hey"
End If
End Sub
Option 2: using the Like operator
If Range("A1").Value Like "correct!" Then
MsgBox "hey"
End If

Running a different macro for a different cell only when changed manually

My problem is that the macros I wrote change the values of the cells triggering again a macro to change one of the other cells.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Target
If Not Intersect(cell, Range("c2")) Is Nothing Then
Macro1
ElseIf Not Intersect(cell, Range("C3")) Is Nothing Then
Macro2
ElseIf Not Intersect(cell, Range("d8")) Is Nothing Then
Macro3
End If
Next cell
End Sub
The macros running always change the other cells, what makes it a endless loop at the moment.
Is there a way to only make manual input/ change of the cell let the macro run?
Two solutions for this :
Add Application.EnableEvents = False at the start of your _change event and set it to True at the end
Create a Public Boolean to test if you are already doing any update automatically
Something like this (solution 2) :
Public DisableEvents As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If DisableEvents Then Exit Sub
DisableEvents = True
Dim cell As Range
For Each cell In Target
If Not Intersect(cell, Range("c2")) Is Nothing Then
Macro1
ElseIf Not Intersect(cell, Range("C3")) Is Nothing Then
Macro2
ElseIf Not Intersect(cell, Range("d8")) Is Nothing Then
Macro3
End If
Next cell
DisableEvents = False
End Sub
Sub Macro1()
If DisableEvents Then Exit Sub
'Rest of your code
End Sub