+ Reply to Thread
Results 1 to 7 of 7

Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

  1. #1
    Registered User
    Join Date
    05-02-2017
    Location
    San Francisco, CA
    MS-Off Ver
    Office 2010
    Posts
    6

    Question Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

    hello! I'm attempting to design a macro that opens an existing workbook, copies a formatted worksheet ("revisions log") into the active workbook as the last sheet, and closes the original source workbook. because I will be calling this macro in multiple workbooks, I need to set the target workbook (variable name TargetFile) as the ActiveWorkbook and will need to store the macro in the Personal Macro Workbook. problem is, when I'm calling a macro from my Personal Macro Workbook, that then becomes the ActiveWorkbook. the attempt to copy the worksheet stops in its tracks.

    I've pasted the code below that does exactly what I want when put in a project module that isn't the Personal Macro Workbook (e.g., "Book1.xlsm"). any suggestions on how to get Excel to recognize that the workbook that was active prior to calling the sub-procedure is what I intend to assign to variable TargetFile?

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

    Hi 146considerations

    If I understand correctly then this is what you are trying to accomplish.
    You have a Personal Macro workbook that contains the code......
    The code must open TargetFile & SourceFile
    Copy Sheet from Sourcefile to Targetfile, closes source file then activates personal Macro workbook to continue with code.....

    Somehow though when you put this in a loop you are going to be overwriting "Revisions"...Not so....Or do you plan to rename active sheet Revisions1 , Revisions2 etc

    Or is this code in your TargetFile?
    Last edited by Sintek; 05-04-2017 at 01:21 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

    I think this might assist:
    Lets assume that you have a Folder on your Desktop called Revisions Folder that houses all you Revisions log workbooks.
    What the code which is in a personal workbook does is as follows:
    Opens your Target workbook on your Desktop which is called target.xlsx for this exercise
    Then Opens the Revisions Folder and opens the first Revisions Log Workbook
    Copies the Sheet called revisons Log to your Target workbook and names it Revisions Log 1
    Then closes that workbook and opens the next in the folder and does the same now only naming the sheet Revisions Log 2
    At the end of the code, you will have a Target file with Sheets called Revisions Log1, 2, 3 , 4 etc dependent on how many files were in the folder.

    Please Login or Register  to view this content.
    Last edited by Sintek; 05-04-2017 at 02:22 AM.

  4. #4
    Registered User
    Join Date
    05-02-2017
    Location
    San Francisco, CA
    MS-Off Ver
    Office 2010
    Posts
    6

    Re: Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

    hi sintek, thanks for your suggestion. let me quickly explain with a bit more detail. I work as a data analyst and build tools for other data analysts in my organization. as I'm building these tools, I want to include a revisions log hidden in the background that I can edit as I make changes, to keep track of when and what I've adjusted over time. so as I'm opening up a new workbook to start building a tool, I want to be able to run a macro straight away that will open up the source file, copy the revisions log worksheet that's formatted how I like it, copy it into the new workbook (TargetFile) then close the source file. I don't want the code to activate the Personal Macro Workbook upon completion; I want it to reference back the new workbook I have open that may or may not have been saved with a name by this stage. there will not be multiple sheets named Revisions, so I don't see a need for loops as it should be a one-iteration action for each workbook.

    that said, it appears that calling a macro saved in the Personal Macro Workbook marks that workbook the active workbook, which then means in my code, when I set the following ...

    Please Login or Register  to view this content.
    ... I'm accidentally setting the Personal Macro Workbook as my ActiveWorkbook instead of the intended workbook. not sure how to rectify this, unless there's a VBA code to activate the most recently activated workbook.. I could sneak that line just before the code embedded in the previous blockquote. but I have no idea if such a command exists or is possible.

    of course there may be a better solution.. just not sure what it would be?

  5. #5
    Registered User
    Join Date
    05-02-2017
    Location
    San Francisco, CA
    MS-Off Ver
    Office 2010
    Posts
    6

    Re: Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

    after writing that, went on a hunt and found a line of code that did exactly what I needed -- to get this to return to previous. only issue now is that when I use the macro shortcut it doesn't run; if I open the macro list, select it and run it, it does it exactly right. I don't know why that is, but I can live with it. changed thread to [SOLVED], and including final code here:

    Please Login or Register  to view this content.
    Last edited by 146considerations; 05-04-2017 at 04:49 PM.

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

    Could just declare the workbook and then activate the one required instead of having two workbooks as active workbooks.

  7. #7
    Registered User
    Join Date
    05-02-2017
    Location
    San Francisco, CA
    MS-Off Ver
    Office 2010
    Posts
    6

    Re: Manipulating ActiveWorkbook when calling macro from Personal Macro Workbook

    this macro does declare the active workbook before opening a second workbook. the problem was calling a macro stored in the Personal Macro Workbook. the second the macro began to execute, the active window shifted from Book1.xlsx (or whatever file I intended to work in) to the Personal Macro Workbook, meaning the macro was declaring the PMW to TargetFile instead of the intended "Book1.xlsx". the fix was to tell the macro to first switch to the previous active window prior to declaring, as that re-focused the attention on "Book1.xlsx" -- that was the bit of code I was missing.

+ 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. Error when calling a 'sheet macro' inside a workbook wide macro
    By Rhino_dance in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-09-2016, 03:46 PM
  2. [SOLVED] Error message when trying to record a macro - No Personal Macro Workbook
    By KimC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-14-2014, 06:56 PM
  3. Replies: 2
    Last Post: 08-13-2014, 06:39 PM
  4. Replies: 1
    Last Post: 08-13-2014, 05:46 PM
  5. Perform macro "on open" specific file- store macro in Personal Macro Workbook?
    By thompssc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2012, 12:38 PM
  6. Macro calling another Macro: "The macro 'Personal.xls!FindChar"
    By William Benson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2005, 09:05 AM
  7. Replies: 2
    Last Post: 03-21-2005, 08:06 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