+ Reply to Thread
Results 1 to 11 of 11

Loop through worksheet names using variables

  1. #1
    Registered User
    Join Date
    06-30-2011
    Location
    Taiwan
    MS-Off Ver
    Excel 2003
    Posts
    6

    Loop through worksheet names using variables

    Hi,

    I have an excel workbook containing 50 worksheets. The names are "Scenario51", "Scenario52"...."Scenario100". Each worksheet contain the same number of data and same format, column/row numbers. All I want to do is write an macro to copy those data from each sheet (from B2 to AA32 in each sheet) and paste transpose to a new worksheet (already created in this workbook called "99AA") from top to bottom and each separate by 6 rows high. My coding is:

    -------------------------------------------------------------------------------
    Sub Test()
    Dim Start As Long
    Dim Sht As String
    For Start = 51 To 100 Step 1

    nwRow = (Start - 51) * 26 + 6 * (Start - 51)
    Sht = "Scenario" & Start
    Worksheets("Sht").Activate
    ActiveSheet.Range("B2:AA32").Copy
    Worksheets("99AA").Range("D2").Offset(nwRow, 0).PasteSpecial xlPasteValues, Transpose:=True

    Next Start

    End

    End Sub
    ---------------------------------------------------------
    It shows an runtime error 9" subscript out of range. I suspect it might be the incorrect way using the variable to loop through different worksheets? Appreciate for any insights.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through worksheet names using variables

    hi,
    change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    no quotes round Sht and no need to activate
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    06-30-2011
    Location
    Taiwan
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Loop through worksheet names using variables

    Hi Joseph, I tries your suggestion and delete the activate but somehow still showing the runtime error 9....

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through worksheet names using variables

    did you remove the quotes round Sht? if so your sheet names are different to what you said. are they like "Scenario51" or "Scenario 51" with a space?

  5. #5
    Registered User
    Join Date
    06-30-2011
    Location
    Taiwan
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Loop through worksheet names using variables

    There is no space in between, the sheet
    name shall be Scenario51 without a quotation

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through worksheet names using variables

    please post current code and confirm which line causes the error

  7. #7
    Registered User
    Join Date
    06-30-2011
    Location
    Taiwan
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Loop through worksheet names using variables

    There you go! Thanks for help.

    Sub Test()
    Dim Start As Long
    Dim Sht As String
    For Start = 51 To 100 Step 1

    nwRow = (Start - 51) * 26 + 6 * (Start - 51)
    Sht = "Scenario" & Start
    Worksheets(Sht).Range("B2:AA32").Copy
    Worksheets("99AA").Range("D2").Offset(nwRow, 0).PasteSpecial xlPasteValues, Transpose:=True

    Next Start

    End

    End Sub


    Te debugging highlighting staying at Sht = "Scenario" & Start

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Loop through worksheet names using variables

    which line causes the error?

  9. #9
    Registered User
    Join Date
    06-30-2011
    Location
    Taiwan
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Loop through worksheet names using variables

    It works! Sorry, I don's know why it didn't but your instruction is right! Thanks so much for this!!!!!

  10. #10
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Loop through worksheet names using variables

    Alternative setup for looping through all sheets - 1 in a workbook:

    Please Login or Register  to view this content.
    Alf

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

    Re: Loop through worksheet names using variables

    altimababe

    Add code tags to your previous posts
    Hope that helps.

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

    Free DataBaseForm example

+ 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