I have a simple question for someone out there.
Can an event macro be somehow entered into a module rather than a worksheet, and thus be called like other macros?
I have a simple question for someone out there.
Can an event macro be somehow entered into a module rather than a worksheet, and thus be called like other macros?
Simple answer? Yes.
There could be a few problems though. They are in different modules, so they may be referenced a little different, but essentially they are the same. If you posted your code we may be able to help tweak it so it will work ok.
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
Fantastic.
I have 2 pieces of code in mind. Here is the first. Hopefully, I can learn to change the second piece. Thanks
Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo ws_exit: Application.EnableEvents = False With Target If .Column = 4 Then Select Case .Value Case Is = "Added" .EntireRow.Interior.ColorIndex = 35 Case Is = "Increased" .EntireRow.Interior.ColorIndex = 20 Case Is = "Deleted" .EntireRow.Interior.ColorIndex = 38 Case Is = "Decreased" .EntireRow.Interior.ColorIndex = 40 End Select End If End With ws_exit: Application.EnableEvents = True End Sub
You might find this helpful...
http://www.cpearson.com/excel/codemods.htm
Thanks xCav8r. The link was helpful in understanding the different macros, although I would still love my code to be reworked if possible.
Firstly, you have to make it Public to be able to access it elsewhere.Originally Posted by jacksonworld
Then you call it prefixed by the sheet name, codename not Excel name, and pass the target range to the event code, e.g.
Sheet5.Worksheet_Change Range("A10")
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
If you just wanted to transfer it straight over, you could adapt it like so ...
As Target is a reference used in change events and not in modules, you just need to explicitly reference it and you can use it once again.Option Explicit Sub ChangeEvent_OLD() Dim Target As Range Set Target = Selection If Target.Cells.Count > 1 Then Exit Sub On Error GoTo ws_exit: Application.EnableEvents = False With Target If .Column = 4 Then Select Case .Value Case Is = "Added" .EntireRow.Interior.ColorIndex = 35 Case Is = "Increased" .EntireRow.Interior.ColorIndex = 20 Case Is = "Deleted" .EntireRow.Interior.ColorIndex = 38 Case Is = "Decreased" .EntireRow.Interior.ColorIndex = 40 End Select End If End With ws_exit: Application.EnableEvents = True End Sub
This does require one to have the cell selected. This can be altered to look at column 4 every time this runs (not necessarily selected) if you'd like.
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
Thanks firefytr, but I am still experiencing some trouble.
I selected the desired cell range and ran your code, yet nothing changed. Am I forgetting to do something?
Ideally, I would like it to look at column 4 every time this runs without making a cell selection, although it is not much of an issue.
Thanks again. I appreciate the assistance.
Hmm, works for me. Let's try the following ...
Note that no sheet is explicitly designated, so that means you must be on the sheet you wish the code to perform on; or explicitly reference the sheet in the Range's.Option Explicit Sub ChangeEvent_OLD() Dim Target As Range, c As Range Set Target = Range("D1", Cells(Rows.Count, "D").End(xlUp)) On Error Goto ws_exit: For Each c in Target Select Case .Value Case = "Added" .EntireRow.Interior.ColorIndex = 35 Case = "Increased" .EntireRow.Interior.ColorIndex = 20 Case = "Deleted" .EntireRow.Interior.ColorIndex = 38 Case = "Decreased" .EntireRow.Interior.ColorIndex = 40 End Select Next ws_exit: End Sub
Regards, Zack Barresse
Check out the KB! :|: BOARD TAGS: WHAT ARE THEY AND HOW DO I USE THEM
What is a Microsoft MVP? | Free Microsoft Courses | My Book on Excel Tables
I managed to get the first code working, but only when I selected one cell at a time.
The second code is displaying a compile error.
We are so close. And when I say we, I mean firefytr.
There's a "With" missing:
Option Explicit Sub ChangeEvent_OLD() Dim Target As Range, c As Range Set Target = Range("D1", Cells(Rows.Count, "D").End(xlUp)) On Error GoTo ws_exit: For Each c In Target With c Select Case .Value Case Is = "Added" .EntireRow.Interior.ColorIndex = 35 Case Is = "Increased" .EntireRow.Interior.ColorIndex = 20 Case Is = "Deleted" .EntireRow.Interior.ColorIndex = 38 Case Is = "Decreased" .EntireRow.Interior.ColorIndex = 40 End Select End With Next ws_exit: End Sub
What's with the Is keyword in the select case?
If you type "=" in the Case, the "Is" is added automatically. Personally I would just have
Case "Added"
but as long as it works ...
Perfect. Thanks for all your help firefytr, and thank you geekgirlau.
No kudos to the peanut gallery? Jeesh!
Sorry. Thanks to xCav8r for the insightful comments.
heehee...victory for the peanut gallery (who had nothing to do with the resolution of this problem)!!!!!!!!!
You rock, son of worldly Jack!!!!
I realise I am being cheeky now, but using my newfound knowledge, I am attempting to convert another event macro... and failing tremendously.
Any chance someone can have a play with this one?
I will paste it as it was originally so as not confuse anyone with my topsy turvy attempts.
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim sh As Worksheet If Not Intersect(Target, Me.Range("F2:F5000")) Is Nothing Then With Target On Error Resume Next Set sh = Worksheets(.Value) On Error GoTo 0 If Not sh Is Nothing Then sh.Activate End If End With End If End Sub
Totally mystified as to why you would do this, but it ain't hard
Public Sub ChangeEvent() Dim sh As Worksheet Dim target As Range Set target = ActiveSheet.Range("F2:F5000") With target On Error Resume Next Set sh = Worksheets(.Value) On Error GoTo 0 If Not sh Is Nothing Then sh.Activate End If End With End If End Sub
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Thanks xld. But I am receiving a compile error on the second last line.
The reasoning behind this macro is that I am using heaps of worksheets and thus compiling a summary page. The linking to worksheets will be perfect (when I get it working ).