+ Reply to Thread
Results 1 to 3 of 3

Make macro on copied sheet refer to the new sheet?

  1. #1
    Registered User
    Join Date
    11-27-2014
    Location
    Ireland
    MS-Off Ver
    2013
    Posts
    2

    Make macro on copied sheet refer to the new sheet?

    Hi All, I have a workbook with a sheet, let's call it "Sheet A", with a macro assigned to a button. This macro adds selected cell contents from "Sheet A" to a separate "Overview" sheet. The macro works fine but if I copy "Sheet A" with the macro assigned button to make a new sheet, "Sheet B", in which to enter different information, the macro in the new sheet, "Sheet B", refers to the information in "Sheet A", and enters the "Sheet A" information into the "Overview" sheet when clicked.

    Is there a simple way, apart from creating new macros each time or copying the macro and editing the code (which would defeat the purpose of the time saving macro), to make the copied macro refer to the information in "Sheet B"?

    Thanks in advance!
    Last edited by ADJetz; 11-27-2014 at 08:53 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: Make macro on copied sheet refer to the new sheet?

    It is certainly possible but there is more than one way to do this depending on how you have structured your existing code. I can't tell you what to change before seeing what you have. Can you attach your file? If it has sensitive data just delete all of the data in all the sheets. I just need to see all the code and where it's located.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-27-2014
    Location
    Ireland
    MS-Off Ver
    2013
    Posts
    2

    Re: Make macro on copied sheet refer to the new sheet?

    No sensitive info, it's a project for college so it has fictional info in it. But i would like to get it working as I could use it in my personal life myself.

    Ill attatch the file now

    I found this on another forum...

    First, declare your Public Variable:
    Code:

    Public MyPrevSheet As String
    Now, in the ThisWorkbook VBA module, create a Workbook_SheetDeactivate to capture your sheet name as you move off it like this:
    Code:
    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    MyPrevSheet = Sh.Name
    End Sub
    Now, you can create a macro (in a Standard Module) that whenever run will go back to the previous sheet, like this:
    Code:

    Sub GoToPreviousSheet()

    If Len(MyPrevSheet) > 0 Then
    Sheets(MyPrevSheet).Activate
    Else
    MsgBox "You have not switched sheets yet since opening the file!"
    End If

    End Sub
    Just assign that code to a button, and that should do it!
    and this will define the name of the most recently deactivated sheet (the one im trying to take info from to put into the "Overview" sheet which gets activated I start the process of linking the data) as a String called "MyPrevSheet".

    There is a similar code I saw which will print the name of the previously deactivated sheet in a message box when you change sheets.

    So if there was some way to get the name of the previously deactivated sheet to be printed into the formula like "='Sheet1'B4", but instead of the "'Sheet1'", have some code to enter the name of the previously deactivated sheet or "MyPrevSheet" in it's place...

    Am I making sense? Ill post the file in a little while, just leaving college now. It's be nice if there was just a button for "Update Macro To Relate To This Sheet"

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. I want to make a macro to bring tables from another sheet to cover sheet
    By nthomas901 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-09-2014, 09:38 AM
  2. Macro to refer to new sheet everymonth with new month name
    By buwa84 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-06-2014, 01:52 PM
  3. [SOLVED] How to refer to the (NAME) of the Sheet rather than the Name of the sheet in my macro
    By Creare in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-11-2013, 05:11 AM
  4. [SOLVED] Refer to a hidden sheet using a macro.
    By fullers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-03-2005, 10:35 AM
  5. Have macro refer to formerly active sheet
    By chaminod in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2005, 06:11 PM

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