trigger an action when new sheet is selected

User avatar
Nick Vittum
4StarLounger
Posts: 475
Joined: 21 Feb 2020, 21:27
Location: Vermont (USA)

trigger an action when new sheet is selected

Post by Nick Vittum »

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.
—Nick

I’m only an egg (but hard-boiled)

User avatar
HansV
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

Post by HansV »

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:

Code: Select all

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    MsgBox "You activated " & Sh.Name
End Sub
S3137.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Nick Vittum
4StarLounger
Posts: 475
Joined: 21 Feb 2020, 21:27
Location: Vermont (USA)

Re: trigger an action when new sheet is selected

Post by Nick Vittum »

Okay, that's exactly what I was looking and hoping for. Perfect. Thanks once again.
—Nick

I’m only an egg (but hard-boiled)

User avatar
Nick Vittum
4StarLounger
Posts: 475
Joined: 21 Feb 2020, 21:27
Location: Vermont (USA)

Re: trigger an action when new sheet is selected

Post by Nick Vittum »

Based on what you told me, I wrote

Code: Select all

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    UF_Categories.Combo_sheets.Value = Sh.Name
End Sub
. 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—

Code: Select all

Private Sub UserForm_Initialize()
    Me.Combo_sheets.Value = Sh.Name
    (etc)
End sub
—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?
—Nick

I’m only an egg (but hard-boiled)

User avatar
HansV
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

Post by HansV »

I will get back to you later.
Best wishes,
Hans

User avatar
Nick Vittum
4StarLounger
Posts: 475
Joined: 21 Feb 2020, 21:27
Location: Vermont (USA)

Re: trigger an action when new sheet is selected

Post by Nick Vittum »

HansV wrote:I will get back to you later.
Thanks. I found it. The proper syntax, apparently, is

Code: Select all

    Me.Combo_sheets.Value = ActiveSheet.Name
Let me know if that's wrong, or if there's a better way. So far, it seems to work.
—Nick

I’m only an egg (but hard-boiled)