How to trigger Worksheet_Change from OLEOjects? - vba

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

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

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

Automatically execute a macrop

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

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

VBA to format cells based on another cell not working

SOLVED: FOUND MY OWN WORKSHEET ERROR
The problem I was having was trying to use two worksheet_change events in the same workbook. Because I thought that was possible, I was just renaming the worksheet event in question when I received an error, thinking nothing of it. Both my original code and the answer provided work, when combined with my other worksheet_change event.
Thanks everyone.
Original Request:
I am trying to run a macro that does this:
every time cell r6 changes, run a macro that looks to see if the value in cell s9 is > or < 1, then format cells s9:t100 based on that.
I have the macro on its own to do the second part:
sub macro1()
If Range("S9").Value < 1 Then
Range("S9:S100,T9:T100").Select
Selection.NumberFormat = "0.0%"
Else
Range("S9:S100,T9:T100").Select
Selection.NumberFormat = "#,##0"
End If
end sub
This macro run on its own, works exactly as I want and formats the cells.
Then I have the worksheet event to call up that macro:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$R$6" Then
Call Macro1
End If
End Sub
When it is run to call up the same macro, it does not format the cells. They just stay as a % regardless of when cell r6 changes.
Any ideas why the worksheet event causes the macro to not work?
Try passing the worksheet object to your macro. This fully qualifies the Ranges to make sure you're working on the right area.
Also, you don't need to Select at all. Just use the range and directly change the settings.
Public Sub Macro1(ws as Worksheet)
If ws.Range("S9").Value < 1 Then
ws.Range("S9:S100,T9:T100").NumberFormat = "0.0%"
Else
ws.Range("S9:S100,T9:T100").NumberFormat = "#,##0"
End If
end sub
Sub test()
Macro1 ActiveSheet
End Sub
And in your Worksheet_Change...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$R$6" Then
Macro1 Target.Worksheet
End If
End Sub