How do get a macro to automatically activate when you click on a sheet.

profklein

New Member
Joined
May 20, 2017
Messages
20
I put the following code in (the macro area of) Sheet 2

Sub Auto_Open()
Sheets("Home").Activate
MsgBox "Welcome "


End Sub

However, nothing happens when I click into Sheet 2.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
.
Both of these macros go into the sheet macro area :

Code:
Option Explicit


Private Sub Worksheet_Activate()
    MsgBox "Hi"
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("D4")) Is Nothing Then
            MsgBox "Hi"
        End If
    End If
End Sub

The top macro will show a msgbox when the sheet is loaded. You go to sheet2, then come back to sheet1.

The bottom macro will show a msgbox if you click on cell D4.
 
Upvote 0
I put the following code in (the macro area of) Sheet 2

Sub Auto_Open()
Sheets("Home").Activate
MsgBox "Welcome "

End Sub

However, nothing happens when I click into Sheet 2.

Hello Profklein

If I'm reading your question correctly, you want a macro that will automatically run the moment you click on the tab for Sheet2. That is quite easy to accomplish. I'm not sure what you mean when you said you put your code in '(the macro area)' and this could be your problem. To make a macro run automatically follow these steps:

1 ) Click on the sheet tab with the RIGHT mouse button. (you MUST use the RIGHT mouse button)
2 ) Select VIEW CODE from the menu that pops up with the LEFT mouse button.
3 ) In the blank Visual Basic editor window, paste this code.
Code:
           Private Sub Worksheet_Activate()
                   MsgBox "Welcome"
           End Sub
4 ) Save and close this code window.
5 ) To test this, click on another sheet tab, then click on the Sheet2 tab as you normally would.
6 ) You should see the message of "Welcome" appear.

I hope this is what you are looking for.

TotallyConfused
 
Last edited:
Upvote 0
Hello Profklein

If I'm reading your question correctly, you want a macro that will automatically run the moment you click on the tab for Sheet2. That is quite easy to accomplish. I'm not sure what you mean when you said you put your code in '(the macro area)' and this could be your problem. To make a macro run automatically follow these steps:

1 ) Click on the sheet tab with the RIGHT mouse button. (you MUST use the RIGHT mouse button)
2 ) Select VIEW CODE from the menu that pops up with the LEFT mouse button.
3 ) In the blank Visual Basic editor window, paste this code.
Code:
           Private Sub Worksheet_Activate()
                   MsgBox "Welcome"
           End Sub
4 ) Save and close this code window.
5 ) To test this, click on another sheet tab, then click on the Sheet2 tab as you normally would.
6 ) You should see the message of "Welcome" appear.

I hope this is what you are looking for.

TotallyConfused


This is exactly what I was looking for. Totally confused - you are NOT confused at all.

Gene
 
Upvote 0
This is exactly what I was looking for. Totally confused - you are NOT confused at all.

Gene
Hello Profklein

Thank you for your feedback. I'm glad I was able to solve your problem. As far as me being 'not confused', I'm not so sure about that. I'm a relative new-comer to Excel and VBA, so compared to some of the other people here, I don't know a single thing about either one. I try to spend time here everyday and study the questions/solutions offered, and there is hardly a day goes by I don't see a command or series of commands used that I'd never heard of before. This site is a virtual goldmine of information.

TotallyConfused (yes, I still am :) )
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top