I'm trying to make a an action occur whenever I shift from one worksheet to another. Would I have to write a macro and then call that macro from every worksheet's activate event? Or is there a way to write it into a Workbook_change event? Or some other way entirely?
In my case, it's a simple bit of code, and only 12 worksheets are affected, so it's not a big deal. But I'm just wondering if my approach is the usual one, or if there's something simpler.
trigger an action when new sheet is selected
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
trigger an action when new sheet is selected
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)
-
- Administrator
- Posts: 78540
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: trigger an action when new sheet is selected
The Workbook object has a Workbook_SheetActivate event. The code for this event goes into the ThisWorkbook module.
The event procedure has an argument Sh that represents the sheet being activated.
Example:
The event procedure has an argument Sh that represents the sheet being activated.
Example:
Code: Select all
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
MsgBox "You activated " & Sh.Name
End Sub
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
Re: trigger an action when new sheet is selected
Okay, that's exactly what I was looking and hoping for. Perfect. Thanks once again.
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
Re: trigger an action when new sheet is selected
Based on what you told me, I wrote. It works perfectly. Whenever I change sheets manually, Combo_Sheets uptdate automatically, and the form data with it. But when I enter a similar line here—
—trying to have the form automatically open to the sheet that's open, it kicks up a fuss. I tried "ActiveSheet.Sh.Name" and "ActiveSheet(Sh.Name)" as well. Nothing I could think of was accepted.
What am I doing wrong?
Code: Select all
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
UF_Categories.Combo_sheets.Value = Sh.Name
End Sub
Code: Select all
Private Sub UserForm_Initialize()
Me.Combo_sheets.Value = Sh.Name
(etc)
End sub
What am I doing wrong?
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)
-
- Administrator
- Posts: 78540
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
Re: trigger an action when new sheet is selected
Thanks. I found it. The proper syntax, apparently, isHansV wrote:I will get back to you later.
Code: Select all
Me.Combo_sheets.Value = ActiveSheet.Name
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)