+ Reply to Thread
Results 1 to 9 of 9

Run macro on this workbook

  1. #1
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Run macro on this workbook

    Hi,

    I have recorded a macro where I run different macros.
    The code looks like this;

    Please Login or Register  to view this content.
    My problem is that sometimes I manually rename the file, and therefor the apllication refering to the old workbookname will not run.

    Is there a way to edit the macro to run on "this workbook", and if there is, can someone please help me modify the code?

    Gnoke
    Last edited by gnoke; 08-13-2010 at 04:18 AM.

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

    Re: Run macro on this workbook

    Look in the VBA Help files at ThisWorkBook & ActiveWorkbook
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Run macro on this workbook

    The VBA help files, in excel or this forum? I do not think I have ever seen them:p

    Gnoke
    Last edited by gnoke; 08-13-2010 at 05:08 AM.

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Run macro on this workbook

    In the VBA editor type in either ThisWorkbook or ActiveWorkbook, leave the cursor somewhere in the word and press F1.

    As you might expect

    Please Login or Register  to view this content.
    Returns the value from cell A1 of the first sheet of the workbook that holds the macro, whereas:

    Please Login or Register  to view this content.
    Returns cell A1 of the first sheet of the workbook that was active when the macro was called. While:

    Please Login or Register  to view this content.
    Gives you cell A1 of whichever sheet was active when you ran the macro.

    I'd say that 99% of the macros I write use ThisWorkbook, ActiveWorkbook or ActiveSheet - they're very useful indeed.

  5. #5
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Run macro on this workbook

    Thanks Andrew-R, that gave me a good overview of these codelines (which I have had some problems understanding in the past)


    Gnoke

  6. #6
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Run macro on this workbook

    I am trying to modify the code above.
    Where schould I write "Msgbox ThisWorkbook.Sheets(1).Range("A1).Value" ?
    Considering that the application is called 98 times, do I have to change this codelines for every call?:
    '120810 printing.xls'!PrintPDF

    Gnoke

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Run macro on this workbook

    If you get rid of the Application.Run bit and the file name and instead just put the line:

    Please Login or Register  to view this content.
    in your macro does that run any better?

  8. #8
    Forum Contributor
    Join Date
    07-21-2010
    Location
    Alta, Norway
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: Run macro on this workbook

    Works like a charm!

    Thank you so much mate

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Run macro on this workbook

    No problems - just don't open another workbook with a different macro, also called PrintPDF in it

+ 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