• For…Next Loop Question (2003)

    Author
    Topic
    #427644

    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

    Viewing 0 reply threads
    Author
    Replies
    • #991425

      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.

      • #991428

        Thank you Hans. I now understand.

        Your explanation was much clearer than my textbook’s!

    Viewing 0 reply threads
    Reply To: For…Next Loop Question (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: