+ Reply to Thread
Results 1 to 14 of 14

Record/write macros independent of a specific file

  1. #1
    Registered User
    Join Date
    08-05-2009
    Location
    ny,ny
    MS-Off Ver
    Excel 2003
    Posts
    23

    Record/write macros independent of a specific file

    Hello I have recorded (i.e. manually as opposed to writing VBA code) a number of macros to perform a routine, however they do not work when I change the filename.

    Could someone please advise on how to edit these macros (which reference a specific filename) so that they work when the file name is changed. The macros copy and paste values from different worksheets and then run another set of macros. However all macros and worksheets are located within one excel file


    Thank you

  2. #2
    Forum Contributor JP Romano's Avatar
    Join Date
    10-09-2008
    Location
    Princeton, NJ
    MS-Off Ver
    2010
    Posts
    500

    Re: How do you record/write macros which work independent of a specific excel file na

    Have you tried replacing the hard coded filename with "ThisWorkbook"?

    Hope that works for you...

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: How do you record/write macros which work independent of a specific excel file na

    Chances are you need to change specific sheet or workbook references to activesheet or activeworkbook.

    Post an example macro.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    08-05-2009
    Location
    ny,ny
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: How do you record/write macros which work independent of a specific excel file na

    Hello Thanks a lot for your suggestions. Require some clarification

    The code (from recording macro) is

    Application.Run "Base_Model.xls!Output_calc_"

    The issue I have is the macro (Output_Calc) references the specific excel workbook filename (Base_Model.xls)

    I would like to alter the code such that if I changed (and saved) the filename of the workbook to say Base_Model_Version_2.xls the macro (Output_Calc) would still run in the saved/renamed file

    Need to know which specific part of code I insert ThisWorkbook (or another suggestion) to make it work.

    Thanks

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How do you record/write macros which work independent of a specific excel file na

    You need to post the code or attach the workbook containing the code.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: How do you record/write macros which work independent of a specific excel file na

    You could create a workbook with no content other than the macros you need. That way, you won't need to change the filename to adapt to your content.

    Or, if all the sheets and macros are in the same workbook, you should only need to enter the macro name

    Please Login or Register  to view this content.
    Last edited by Whizbang; 10-21-2009 at 08:53 AM.

  7. #7
    Registered User
    Join Date
    08-05-2009
    Location
    ny,ny
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Record/write macros independent of a specific file

    Hello I have attached a file which illustrates teh problem. Essentially the second macro (Ctrl + Shift+ A) does not work if I change the file name because the first macro which it activates references the original filename. Any help providing exact instructions to change macro to make this work indepenbent of file name is appreciated. Thank you

  8. #8
    Registered User
    Join Date
    08-05-2009
    Location
    ny,ny
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Record/write macros independent of a specific file

    forgot to attach file. attached now
    Attached Files Attached Files

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Record/write macros independent of a specific file

    Looking at your code it's hard to follow. I think it would be much easier for you to explain step by step what you want the code to do

  10. #10
    Registered User
    Join Date
    08-05-2009
    Location
    ny,ny
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Record/write macros independent of a specific file

    Thanks

    First Macro (ctrl +A) is a goal seek function: sets s10 = s13 by altering G8

    Second macro (Ctrl + shift + A) uses first macro in creating a matrix in sheet A (example attached only creates 4 points in the matrix) by entering adjustment factors in B6 and B7 in sheet B (the adjustemnt factor values are obatined from the matrix row and column headers in sheet A

    Adj factors change s10 automatically so macro 2 then runs macro 1 (goal seek) to set s10 back to oiginal target of s13 by altering G8

    Example selecting values in cells B1 (-5%)and A2 (05) in Sheet A and pasting them in cells B6 B7 in sheet B will produce a value of 2250. So cell g8 has to be altered (goal seek) until 2250 is reduced to 50 (i.e the target). So new value for G8 is 12.28 (not original value of 20)

    Main point however is cannot use Sensitivity Macro (2) because OptCalc macro (1) is filename (Mexa.xls) dependent. This issue is independent of the routine the macro performs. Hope this is clear thanks

  11. #11
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: Record/write macros independent of a specific file

    I am no expert, I cannot even follow your code completely, but replacing

    Please Login or Register  to view this content.
    With

    Please Login or Register  to view this content.
    seems to work just fine. I didn't get any errors, at least.


    You can even shorten it to just

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    08-05-2009
    Location
    ny,ny
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Record/write macros independent of a specific file

    Thanks a lot. Issue is resolved now

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Record/write macros independent of a specific file

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  14. #14
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: Record/write macros independent of a specific file

    I edited and cleaned up your code a bit. You don't need to select cells to work with them. Also, by specifying sheets, you avoid running your macro from the wrong sheet. If sheet B was active and I ran the code you provided, I would get a much different result than if sheet A was active.

    Also, I put both sub-routines into one module.

    I am still at a total loss as to what the purpose of your code is, but at least it's a little cleaner.

    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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