+ Reply to Thread
Results 1 to 3 of 3

Referring to the previous selected sheet in a macro

  1. #1
    michaelberrier
    Guest

    Referring to the previous selected sheet in a macro

    Is there anyway to refer to the previous selected sheet in a macro?

    In other words, the macro is designed on a sheet named "Master" and
    switches back and forth between that sheet and another, lets call it
    "Output".

    "Master" is designed to be copied and renamed, but it always references
    "Output" in certain macros.

    The code currently begins on "Master" and goes something like this:

    Range("A1:A10").Copy
    Sheets("Output").Select
    Range("A1:A10").Paste
    Sheets("Master").Select
    ....and so on.

    I need that same functionality if "Master" is named "Master2" or
    "AbeVigoda" or whatever. Does VBA include syntax for Previous Sheet or
    Last sheet or anything so that you can tell the macro to go back to
    sheet it started from?

    Thanks to all.


  2. #2
    Jim Cone
    Guest

    Re: Referring to the previous selected sheet in a macro

    Assuming the ActiveSheet is the start sheet then just assign the
    sheet name to a string variable....
    Dim strSheetName as String
    strSheetName = ActiveSheet.Name
    Use it like this...
    WorkSheets(strSheetName).Range("xyz").Copy
    -or-
    Set an Object Variable to the ActiveSheet...
    Dim objSheet as Excel.Worksheet
    Set objSheet = ActiveSheet
    Use it like this...
    ObjSheet.Range("xyz").Copy
    --
    Jim Cone
    San Francisco, USA
    http://www.realezsites.com/bus/primitivesoftware


    "michaelberrier"
    <michaelberrier@gmail.com>
    wrote in message
    Is there anyway to refer to the previous selected sheet in a macro?
    In other words, the macro is designed on a sheet named "Master" and
    switches back and forth between that sheet and another, lets call it
    "Output".
    "Master" is designed to be copied and renamed, but it always references
    "Output" in certain macros.
    The code currently begins on "Master" and goes something like this:

    Range("A1:A10").Copy
    Sheets("Output").Select
    Range("A1:A10").Paste
    Sheets("Master").Select
    ....and so on.
    I need that same functionality if "Master" is named "Master2" or
    "AbeVigoda" or whatever. Does VBA include syntax for Previous Sheet or
    Last sheet or anything so that you can tell the macro to go back to
    sheet it started from?
    Thanks to all.


  3. #3
    michaelberrier
    Guest

    Re: Referring to the previous selected sheet in a macro

    Absolutely PERFECT! Thanks a lot.

    Jim Cone wrote:
    > Assuming the ActiveSheet is the start sheet then just assign the
    > sheet name to a string variable....
    > Dim strSheetName as String
    > strSheetName = ActiveSheet.Name
    > Use it like this...
    > WorkSheets(strSheetName).Range("xyz").Copy
    > -or-
    > Set an Object Variable to the ActiveSheet...
    > Dim objSheet as Excel.Worksheet
    > Set objSheet = ActiveSheet
    > Use it like this...
    > ObjSheet.Range("xyz").Copy
    > --
    > Jim Cone
    > San Francisco, USA
    > http://www.realezsites.com/bus/primitivesoftware
    >
    >
    > "michaelberrier"
    > <michaelberrier@gmail.com>
    > wrote in message
    > Is there anyway to refer to the previous selected sheet in a macro?
    > In other words, the macro is designed on a sheet named "Master" and
    > switches back and forth between that sheet and another, lets call it
    > "Output".
    > "Master" is designed to be copied and renamed, but it always references
    > "Output" in certain macros.
    > The code currently begins on "Master" and goes something like this:
    >
    > Range("A1:A10").Copy
    > Sheets("Output").Select
    > Range("A1:A10").Paste
    > Sheets("Master").Select
    > ...and so on.
    > I need that same functionality if "Master" is named "Master2" or
    > "AbeVigoda" or whatever. Does VBA include syntax for Previous Sheet or
    > Last sheet or anything so that you can tell the macro to go back to
    > sheet it started from?
    > Thanks to all.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1