VBA if active sheet name is

  • Thread starter PATSYS
  • Start date Oct 18, 2009

  • #1

Hi all,

I have codes residing in workbook "File1.xls". In this workbook I have a sheet with sheet codename "Sheet1".

I would like my code to run only if the "Sheet1" is the active sheet.

In short, I would like to add a line in my code that says something like:

Code:

If Thisworkbook.Sheet1=ActiveSheet then ...

Can someone show me that correct code to achieve this?

Thanks

Control Word Wrap

Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

  • #2

Hi PATSYS

Is it

Code:

If Activesheet.Name = "Sheet1" Then .......

  • #3

Hi Dave,

Thanks for your post.

However the "Sheet1" is the sheet codename, not the sheetname.

I prefer to use the sheet codename becauise the sheetname is changing all the time.

Do you know how this is done?

Thanks

  • #4

Dave, I got it.

Code:

If ActiveSheet.CodeName = "Sheet1" Then

Thanks for pointing me to the right direction.

Threads1,180,536Messages5,925,058Members436,387Latest memberBulugoy

  1. 08-02-2006, 11:40 AM #1

    If activesheet is called X, then do y


    Hi guys,

    I need some VBA that says if the active sheet is "dogs", then call macro
    entitled get dogs. If active sheet is "cats", then call macro entitled
    "get Cats".

    Possible...?

    Thanks!!

    D

    *** Sent via Developersdex http://www.developersdex.com ***


  2. 08-02-2006, 11:45 AM #2

    Re: If activesheet is called X, then do y

    Assuming everything exists..
    Run ActiveSheet.Name

    Charles
    xl Geek

    Darin Kramer wrote:


    > Hi guys,
    >
    > I need some VBA that says if the active sheet is "dogs", then call macro
    > entitled get dogs. If active sheet is "cats", then call macro entitled
    > "get Cats".
    >
    > Possible...?
    >
    > Thanks!!
    >
    > D
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***


  3. 08-02-2006, 11:45 AM #3

    Re: If activesheet is called X, then do y

    One way I think will work is:

    if activesheet.name = "dogs" then GetDogs
    if activesheet.name = "cats" then GetCats

    However, when is this code supposed to run. Is it based on anytime the
    sheet is active, or is it dependent on some other code that runs?
    If it is to run any time those sheets are activated then you could add code
    specifically to that sheet.
    For example:

    Right-click on sheet "dogs" and select View code. Click General dropdown
    and select Worksheet. Click other dropdown and make sure it is on
    Activate".
    Then in your code, enter the macro name you want to run when this sheet is
    selected. Here's what it would look like:

    Private Sub Worksheet_Activate()
    GetDogs
    End Sub

    HTH,
    Paul

    "Darin Kramer" <> wrote in message
    news:...


    >
    > Hi guys,
    >
    > I need some VBA that says if the active sheet is "dogs", then call macro
    > entitled get dogs. If active sheet is "cats", then call macro entitled
    > "get Cats".
    >
    > Possible...?
    >
    > Thanks!!
    >
    > D
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***


  4. 08-02-2006, 12:00 PM #4

    Re: If activesheet is called X, then do y

    One way I think will work is:

    if activesheet.name = "dogs" then GetDogs
    if activesheet.name = "cats" then GetCats

    However, when is this code supposed to run. Is it based on anytime the
    sheet is active, or is it dependent on some other code that runs?
    If it is to run any time those sheets are activated then you could add code
    specifically to that sheet.
    For example:

    Right-click on sheet "dogs" and select View code. Click General dropdown
    and select Worksheet. Click other dropdown and make sure it is on
    Activate".
    Then in your code, enter the macro name you want to run when this sheet is
    selected. Here's what it would look like:

    Private Sub Worksheet_Activate()
    GetDogs
    End Sub

    HTH,
    Paul

    "Darin Kramer" <> wrote in message
    news:...


    >
    > Hi guys,
    >
    > I need some VBA that says if the active sheet is "dogs", then call macro
    > entitled get dogs. If active sheet is "cats", then call macro entitled
    > "get Cats".
    >
    > Possible...?
    >
    > Thanks!!
    >
    > D
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***


  5. 08-02-2006, 12:20 PM #5

    Re: If activesheet is called X, then do y

    Oops I forgot the "get"
    Run "get" & Activesheet.Name
    I eliminated the space because I don't think excel will let you have a
    space in a macro name.

    Charles
    xl Geek
    Die_Another_Day wrote:


    > Assuming everything exists..
    > Run ActiveSheet.Name
    >
    > Charles
    > xl Geek
    >
    > Darin Kramer wrote:
    > > Hi guys,
    > >
    > > I need some VBA that says if the active sheet is "dogs", then call macro
    > > entitled get dogs. If active sheet is "cats", then call macro entitled
    > > "get Cats".
    > >
    > > Possible...?
    > >
    > > Thanks!!
    > >
    > > D
    > >
    > >
    > > *** Sent via Developersdex http://www.developersdex.com ***


  6. 08-02-2006, 02:50 PM #6

    Re: If activesheet is called X, then do y

    I had forgotten to take out a line. Here is the version that works. Sorry
    about that.

    Sub fill_in()
    ActiveSheet.Range("A3").Select
    For Each cell In Sheets
    Do
    If ActiveCell > "" Then

    ElseIf ActiveCell.Offset(0, 3) > "" Then
    ActiveCell = ActiveCell.Offset(-1, 0)

    ElseIf ActiveCell = "" And ActiveCell.Offset(1, 0) = "" Then
    Exit For
    End If
    ActiveCell.Offset(1, 0).Activate
    Loop
    Next
    End Sub

    --
    Best wishes,

    Jim

    "PCLIVE" wrote:


    > One way I think will work is:
    >
    > if activesheet.name = "dogs" then GetDogs
    > if activesheet.name = "cats" then GetCats
    >
    > However, when is this code supposed to run. Is it based on anytime the
    > sheet is active, or is it dependent on some other code that runs?
    > If it is to run any time those sheets are activated then you could add code
    > specifically to that sheet.
    > For example:
    >
    > Right-click on sheet "dogs" and select View code. Click General dropdown
    > and select Worksheet. Click other dropdown and make sure it is on
    > Activate".
    > Then in your code, enter the macro name you want to run when this sheet is
    > selected. Here's what it would look like:
    >
    > Private Sub Worksheet_Activate()
    > GetDogs
    > End Sub
    >
    >
    > HTH,
    > Paul
    >
    >
    > "Darin Kramer" <> wrote in message
    > news:...
    > >
    > > Hi guys,
    > >
    > > I need some VBA that says if the active sheet is "dogs", then call macro
    > > entitled get dogs. If active sheet is "cats", then call macro entitled
    > > "get Cats".
    > >
    > > Possible...?
    > >
    > > Thanks!!
    > >
    > > D
    > >
    > >
    > > *** Sent via Developersdex http://www.developersdex.com ***

    >
    >
    >


What is the sheet name of the active worksheet?

What is Active Sheet in Excel VBA. Worksheet which is currently activated in the Active Workbook and Active Window is referred as Active Sheet. You can make any Worksheet as Active Worksheet by Activating a Worksheet. You can use Activate Method of Worksheet to activate a sheet using Excel VBA.

How can I tell if a sheet is active in Excel?

How to quickly show active or selected sheets only in Excel?.
Press Alt + F11 keys to display Microsoft Visual Basic for Applications window..
Then click Insert > Module and paste below VBA to the new Module window. ... .
Click Run button or press F5 key to execute VBA And now only active sheet are shown, others are hidden..

What is current worksheet?

Similarly, the active sheet or current sheet is the worksheet containing the active cell. Like the active cell, the active sheet is considered to have focus when it comes to performing actions that affect one or more cells — such as formatting — and the changes occur to the active sheet by default.

What does active sheet mean in VBA?

The ActiveSheet is the worksheet tab that is currently selected before running the macro. If multiple sheets are selected, the ActiveSheet is the sheet that is currently being viewed.