+ Reply to Thread
Results 1 to 6 of 6

Writing a macro that pulls data from a specific worksheet

  1. #1
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105

    Writing a macro that pulls data from a specific worksheet

    Is there a way to use the count function in VBA to pull data from the 8th worksheet in a workbook? I can't use the specific name of the worksheet because its liable to change from time to time. As always, thanks in advance for the help.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    The following earlier post on this forum describes sheet naming conventions.

    http://www.excelforum.com/showthread.php?t=643863

    If by the 8th sheet you mean sheet tabs counting from the left, then use the Sheets(8) syntax with e..g.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    02-08-2004
    Posts
    58
    This might be a stupid response (I may not understand exactly what I am talking about) :-) but wouldn't the 8th sheet always be example, "sheet8" (That is, assuming that it was originally made as sheet8 or named sheet8) no matter what excel name the tab has ???

    sheet8.range("a1").value is the same no matter what excel names sheet8, unless you change the sheet name in VB properties ... Right ?


    Or maybe I should mind my own business so I don't look stupid haha ,,,

    Later ...
    Sig ? How can I sign the computer screen ?

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    No, it's not being stupid, the syntax Microsoft chose is not particularly intuitive in my opinion and I'm not suprised it trips people up - as indeed it has with me on several occasions.

    You need to distinguish between the sheet names and indexes as each one is added to the workbook, and the order of the sheet tabs that exist at any one time - and of course these can be moved around.

    In the first case, as sheets are added, they are given an incremental index number, and each new sheet is given the default name Sheet1, Sheet2 Sheet3 etc. This remains as a VBA reference whatever name you subsequently change it to, Thats why in the VBE you see both the Sheet Index 'name' and the tab name. So for instance if you add three sheets and name them A, B, C as you add them, you can subsequently refer to them in code as either Sheet1, Sheet2, Sheet3, or Sheets("A"), Sheets("B"), Sheets("C").

    Now if you make the 'B' sheet the last sheet, i.e. the third from the left, if you use code like Sheets(3).Activate, it will be the 'B' sheet that's activated, and not the third sheet that happened to be added to the workbook.

    The subtle distinction is between 'Sheet3.Activate' and 'Sheets(3).Activate'

    HTH

  5. #5
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105
    Ahh thats really good to know, thanks guys!

  6. #6
    Registered User
    Join Date
    02-08-2004
    Posts
    58
    Ahhh I see the ()'s ... Interesting...

    Good information, i'm glad I stirred it out on accident haha ...

+ 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