+ Reply to Thread
Results 1 to 5 of 5

Loop not running through

Hybrid View

bk23 Loop not running through 05-14-2019, 06:25 AM
TMS Re: Loop not running through 05-14-2019, 07:08 AM
Norie Re: Loop not running through 05-14-2019, 07:30 AM
bk23 Re: Loop not running through 05-15-2019, 03:51 AM
TMS Re: Loop not running through 05-15-2019, 04:08 AM
  1. #1
    Registered User
    Join Date
    05-14-2019
    Location
    London
    MS-Off Ver
    2013
    Posts
    2

    Loop not running through

    Hi Forum experts, first post!

    Trying to get 3 specified sheets from one work book copied over to another workbook.

    Set up an array and loop and it runs through the first loop but gets stuck with a subscript out of range error at the Set in the second loop.

    Dim array1(2) As String
    array1(0) = "source1"
    array1(1) = "source2"
    array1(2) = "source3"
    
    Dim mtabname As Worksheet
    Dim i As Variant
    
    For Each i In array1
    Set mtabname = Worksheets(i)
    Sheets(i).Select
    Sheets(i).Copy after:=Workbooks("target.xlsm").Sheets(Workbooks("target.xlsm").Worksheets.Count)
    Sheets(Sheets.Count).Name = i
    
    Next
    Any idea what I'm doing wrong?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,359

    Re: Loop not running through

    I think this should do what you want … if that is to copy three sheets to the Target.xlsm workbook.

    Sub Test()
    
    Dim array1, i
    array1 = Array("source1", "source2", "source3")
    
    For Each i In array1
        ThisWorkbook.Sheets(i).Copy after:=Workbooks("target.xlsm").Sheets(Workbooks("target.xlsm").Worksheets.Count)
    Next
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Loop not running through

    No need for a loop, this will copy 'source1', 'source2' and 'source3' from the active workbook to 'target.xlsm'.
    array1(0) = "source1"
    array1(1) = "source2"
    array1(2) = "source3"
    
    Sheets(array1).Copy after:=Workbooks("target.xlsm").Sheets(Workbooks("target.xlsm").Worksheets.Count)
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    05-14-2019
    Location
    London
    MS-Off Ver
    2013
    Posts
    2

    Re: Loop not running through

    used a variation of Norie's code, thanks to both!

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,359

    Re: Loop not running through

    You're welcome.

+ 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. [SOLVED] Loop keeps running
    By jduong93 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2016, 07:30 PM
  2. [SOLVED] VBA loop not running properly
    By joe_edd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-08-2016, 07:26 AM
  3. [SOLVED] Loop not running on all worksheets
    By usmc1sok in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 09-12-2013, 01:12 PM
  4. Do loop not running
    By hattrick_123a in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-21-2011, 08:36 PM
  5. Loop not running
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2006, 10:26 AM
  6. For Each Loop - running three times
    By Rick A in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-23-2006, 01:30 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