+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    11-27-2008
    Location
    Manila, Philippines
    Posts
    20

    Loop for all Worksheets

    hELP ME SO i CAN run THIS codeS to all worksheets in the activeworkbook AT ONE TIME,(e.i. Oct 2008). I record this macro IN my Personal macros as 'Macro5.'
    Code:
       Windows("IDF Clearing Template REVISED.xls").Activate
        Rows("1:2").Select
        Range("BE1").Activate
        Selection.Copy
        Windows("Oct 2008.xls").Activate
        Rows("1:1").Select
        Range("BG1").Activate
        Selection.Insert Shift:=xlDown
        Windows("IDF Clearing Template REVISED.xls").Activate
        Columns("BM:BQ").Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Oct 2008.xls").Activate
        Application.WindowState = xlMinimized
        Columns("BM:BM").Select
        ActiveSheet.Paste
        Range("BN2").Select
        Application.Run "'IDF Clearing Template REVISED.xls'!Macro1"

    Thanks and best regards
    Last edited by royUK; 12-08-2008 at 11:41 AM.

  2. #2
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    Posts
    1,609
    Is this what you want?

    Code:
    Dim sh As Worksheet
    Dim target As Worksheet
    
        Set target = Workbooks("Oct 2008.xls").Worksheets(1)
        For Each sh In Workbooks("IDF Clearing Template REVISED.xls")
        
            sh.Range("BE1").Copy
            target.Range("BG1").Insert Shift:=xlDown
            sh.Columns("BM:BQ").Copy target.Columns("BM:BM")
            SHRange("BN2").Select
            Application.Run "'IDF Clearing Template REVISED.xls'!Macro1"
        Next sh

  3. #3
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,443
    Your post does not comply with Rule 3 of our Forum Rules. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here http://www.excelforum.com/misc.php?do=bbcode#code

    Note: I will add them this time only!
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  4. #4
    Registered User
    Join Date
    11-27-2008
    Location
    Manila, Philippines
    Posts
    20
    HI.

    my working file is the 'Oct 2008.xls'. In each Worksheet, I want to run the the 'macro5' located in my "Personal Macros.bas".

    Thanks

  5. #5
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,443
    Is this whay ytou mean, place the code in the workbook you are using

    Code:
     Dim ws     As Worksheet
    
        For Each ws In ThisWorkbook.Worksheets
            Application.Run "'Personal.xls'!Macro5"
        Next ws
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  6. #6
    Registered User
    Join Date
    11-27-2008
    Location
    Manila, Philippines
    Posts
    20
    Quote Originally Posted by royUK View Post
    Is this whay ytou mean, place the code in the workbook you are using

    Code:
     Dim ws     As Worksheet
    
        For Each ws In ThisWorkbook.Worksheets
            Application.Run "'Personal.xls'!Macro5"
        Next ws

    Hi. The Macro5 codes only works for active worksheet not for the rest of all non-empty worksheets.

    Thanks.

  7. #7
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,443
    Then you either need to amend macro5 to work efficiently to suit your needs or amend my code to actually select each sheet, which will of course be slower & less efficientr
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0