Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Dec 2004
    Location
    Stoney Creek, Ontario
    Posts
    196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For...Next Loop Question (2003)

    I'm trying to teach myself VBA and it isn't proving to be easy. In the macro I have, I want the code to loop through 3 different worksheets and copy data to dump into a 4th worksheet. Right now, it's looping more times than I want it to and I can't figure it out. Here is the code that I'm using from my workbook:

    For X = 1 To Worksheets.Count - 1

    What exactly does the "Count-1" part mean?

    Thanks in advance.

    Deb

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: For...Next Loop Question (2003)

    Worksheets is the collection of all worksheets in your workbook.
    Worksheets.Count is the number of worksheets. So if you have 4 worksheets, Worksheets.Count will be 4, and hence Worksheets.Count - 1 will be 3.
    For X = 1 To Worksheets.Count - 1 means that X will take on the values 1, 2, ... etc. up to and including 1 less than the number of worksheets. If there are 4 worksheets, X will be 1, 2 and 3.

  3. #3
    2 Star Lounger
    Join Date
    Dec 2004
    Location
    Stoney Creek, Ontario
    Posts
    196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: For...Next Loop Question (2003)

    Thank you Hans. I now understand.

    Your explanation was much clearer than my textbook's!

Posting Permissions

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