Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Melbourne, Fl USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reference First Worksheet (Excel 2003)

    I have several different workbooks that I could run the same code on except for the fact that they do not all have the same nbr of worksheets. In VBA, how do I reference the first worksheet or the last worksheet?

    thx in advance

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference First Worksheet (Excel 2003)

    The first worksheet is:

    <pre>WorkSheets(1)
    </pre>


    The last worksheet is:

    <pre>Worksheets(Worksheets.Count)
    </pre>


    If you want to do the same operation on every worksheet in the workbook, then you can use the For Each statement, like this:

    <pre>Dim oSheet as WorkSheet
    For Each OSheet In WorkSheets
    oSheet.Range("A1").Value = "This sheet is named " & oSheet.Name
    Next oSheet
    </pre>

    Legare Coleman

  3. #3
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Melbourne, Fl USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thx

    Works perfectly - and thanks for the for/each suggestion. I know it will be useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •