I am trying to make it so that when one of the specific cells in my code is changed then it will display a message but i am getting the following error message "run time error 438 object doesnt suport this property or method". Not realy sure what this means. could someone please help me understand. Here is the code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Adress = "F48,I48,L48,F50,I50,L50,I52,L52,N52" Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
1st, as mentioned in comment, use Target.Address which is correct property name.
2nd, your if statement will never return true. Target.Address will always return something like this: $E$2, $E$3:$E$4, and so on... In your situation you should use something like Intersect or Union methods.
Edited- possible solution using Union method:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngTMP As Range
Set rngTMP = Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")
If Union(Target, rngTMP).Address = Union(rngTMP, rngTMP).Address Then
MsgBox "Ok"
End If
End Sub
Is this what you are trying?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")) Is Nothing Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
Worth Reading: MS Excel crashes when vba code runs
The Code Siddharth Gave works wonders so Thanks so much. I was running into trouple because I was trying to make two Worksheet_Change events by writing the code the following way:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$8" Then
Toggle_Rows
End If
End Sub
Private Sub Worksheet_Change(Byval Target As Range)
If Not Intersect(Target, Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")) Is Nothing Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
As you can imagine this did not work, gave me an Ambiguous name error. So after some research the following is the way two write these two functions as one:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$8" Then
Toggle_Rows
End If
If Not Intersect(Target, Range("F48,I48,L48,F50,I50,L50,I52,L52,N52")) Is Nothing Then
MsgBox "You are about to change an AP-42 Emision Factor"
End If
End Sub
Thanks for all the help everyone!
Related
I am trying to create a subroutine that runs when any cell in column 13 is changed. I've read a number of stack overflow questions already, but have not found my answer. here are some I've read:
Not activated by a change
Uses intersect. Doesn't address my issue
Might work but I'm not very good with events
I tried to make it work using the intersect fuction
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect (Target, Activesheet.Columns(13)) Is Nothing Then
MsgBox "Help Help"
EndIf
End Sub
This works when I change values but if left alone for a while it will come up with "Run-time error '1004': Method 'intersect of object '_Global' failed". Any ideas welcome. if there is a simpler way to acheive this, I'd love to know. Thank you for your time.
to handle ALL worksheet, place this in ThisWorkbook code pane
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Sh.Columns(13)) Is Nothing Then
MsgBox "Help Help"
End If
End Sub
or
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column = 13 Then
MsgBox "Help Help"
End If
End Sub
Otherwise place the following in the code pane of the Sheet(s) you want to "handle" only
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 13 Then
MsgBox "Help Help"
End If
End Sub
I have a problem with Worksheet_Change. When I target the cell Excel doesn't react at all even if I put an error in the code. I put the Worksheet_Change on the right sheet, so that's not the reason.
I simplified my code as much as I could and there is still no reaction.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$c$2" Then
MsgBox "hi"
End If
End Sub
I think the event listener that you want to trigger is Worksheet_SelectionChange.
For Worksheet_Change to work, you need to type something in cell C3
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Me.Range("C2"), Target) Is Nothing Then
MsgBox "Great! Target is within the chosen range!"
End If
End Sub
This works. I always use the above structure.
Is there an object and/or function that can detect or represent a general change on a worksheet? Something similar to (for example) ComboBox1_Change() but could be applied to a whole worksheet. Almost something like Worksheet1_Change(). Any suggestions are welcome. Thanks.
The simplest would be to use the worksheet change event:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "Change Detected!"
End Sub
There are instances will this will throw you in to an infinite loop. Consider this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Range("A1").Value = "Change"
End Sub
Set a breakpoint on the Range("A1") code and make a change somewhere else on the sheet and count how many time that breakpoint gets hit. When you get tired of hitting F5, stop the code and try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Range("A1").Value = "Change"
Application.EnableEvents = True
End Sub
You'll see that the code only fires once. Which is probably what you're after.
It's very important that you have the Application.EnableEvents = True line in there, or else you'll get the appearance that your code isn't working (by default EnableEvents does not revert back to True at the end of the code).
I am trying to call a macro named "RE_environmental" when the cell named "RE_1" is changed (i.e. they mark a X in the cell). I've tried several different variations of codes including these two and nothing is happening:
[The first code does work if I use the exact cell location and not the named cell. --> $E$62]
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "RE_1" Then
Call RE_environmental
End If
End Sub
AND
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("Name").Select = "RE_1" Then
Call RE_environmental
End If
End Sub
--Thanks in Advanced and please let me know if you need more information.
Use your first answer but make this small change:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("RE_1").Address Then
Call RE_environmental
End If
End Sub
Simple syntax mistake!
Edit: To stop RE_environmental from running once the cell is empty, put the code from RE_environmental inside a do-while (not isempty(Range("RE_1"))) as long as RE_environmental is emptying "RE_1". The user won't be able to edit cells while RE_environmental is running.
If Target is always single-cell range, you can use this one:
If Target.Address = Range("RE_1").Address Then
Call RE_environmental
End If
If Target can be multicell range use this one:
If Not Intersect(Target, Range("RE_1")) Is Nothing Then
Call RE_environmental
End If
Hi I am trying to make a search page on an on-line excel (www.editgrid.com) using macro to lock all cells except for (B2 cell) and each time the page is opened it clears / resets (B2 cell) and I keep getting this error Missing ; before statement at line 1 Private Sub Worksheet_SelectionChange(ByVal Target As Range) how do I fix this error. Thank you
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Selection, Range("A1:AS57")) Is Nothing Then
Range("B2").Select
End If
End Sub
Option Explicit
Private Sub Worksheet_Activate()
[search_string] = "Type your search here."
[search_string].Select
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Const CRITERIA_HEADER = "Description"
Const TEMPORARY_NAME_CREATED_BY_ADVANCED_FILTER = "Extract"
' If change was from any cell other than our lookup, then exit
If Intersect(Target, [search_string]) Is Nothing Then Exit Sub
[search_string].Select
End Sub
It would appear that macros for EditGrid should be written in JavaScript and not in VBA. See this guide for more details