+ Reply to Thread
Results 1 to 8 of 8

Using Array for Sheet Names

  1. #1
    Forum Contributor
    Join Date
    02-22-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    102

    Using Array for Sheet Names

    hello,

    i have names of sheets stored in Shno1, shno2,shno3 and so on. I would like to refer this in a for loop with increment variable (i) and the following code is either incorrect on logic / syntax. can i get the right one please.

    Workbooks(firstfile).Sheets(shno(i)).UsedRange.Copy

    Ex:
    i have Sheetx, SheetY, SheetZ stored in variable shno1,shno2,shno3 respectively and i have to use the above code which should refer to these sheets.

    Thanks.

  2. #2
    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: Using Array for Sheet Names

    shno needs to be an array containing the names of sheets. You can't reference a scalar variable like that.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    02-22-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    102

    Re: Using Array for Sheet Names

    sorry If iam missing something or not been clear on my earlier query

    i have defined shno as array and stored the names of the sheets in it

    Please Login or Register  to view this content.
    Note: i have stored the sheetnames in D1,D2 etc..

  4. #4
    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: Using Array for Sheet Names

    Then if firstfile is a valid name for an open workbook, and shno contains valid sheet names, it should work fine.

    However, the last code you posted doesn't compile. The loop index is ctr, not i, and the variable that follows the Next must be the loop variable, and you should never change the loop index variable (ctr=ctr+1) inside a loop.
    Last edited by shg; 03-22-2013 at 07:19 PM.

  5. #5
    Forum Contributor
    Join Date
    02-22-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    102

    Re: Using Array for Sheet Names

    my apologies again. Not sure if posting the entire code may be relevant. Hence I am redefining my query. If this helps it would be great else i will paste my complete code.

    for instance if i have to refer to display values in D2 of all sheets and use Sheetnames to refer ( instead of referring to sheet1,sheet2 etc). how do i do that?
    the one below would require change as it is not working
    Please Login or Register  to view this content.

  6. #6
    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: Using Array for Sheet Names

    That refers to D2 on one specific worksheet. You cannot refer to D2 on multiple sheets.

  7. #7
    Forum Contributor
    Join Date
    02-22-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    102

    Re: Using Array for Sheet Names

    Thanks for your constant responses inspite of me not getting my query right. Let me add to what i had posted before

    Sheetname Value in D2
    Abc 50
    bcd 100
    cde 150
    def 200

    i have the sheet names stored in Shno1,shno2,shno3,shno4 as abc,bcd,cde,def respectively. when i run this code and when it loops every time should display 50 first [ refers to Sheet "Abc" as this is stored in shno(1), 100 second time ( refers to "Bcd" as this is stored in Shno(2), 150 third time and 200 on the final loop
    Please Login or Register  to view this content.
    Hope this is clear.

  8. #8
    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: Using Array for Sheet Names

    And -- what happens?

    Does the code error, do nothing, do something wrong ...

+ 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