+ Reply to Thread
Results 1 to 10 of 10

Loop through worksheets

  1. #1
    Registered User
    Join Date
    07-17-2008
    Location
    Moon
    Posts
    8

    Loop through worksheets

    I have a workbook that has a summary page and an undetermined amount of worksheets. I'd like to iterate through the worksheets, updating a formula on the summary page. I've included a sample to help explain...

    Basically, I'd like to get the average of all of the numbers in cell B2 for each worksheet and display the average on the summary tab. I will be adding tabs through the month that will need to be included on the summary tab. The format for all new tabs will remain the same, so each new tab will still reference cell B2.

    Help is greatly appreciated...
    Attached Files Attached Files

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    No need for code

    See attached. Place any need sheets between the start and end sheets

    VBA Noob
    Attached Files Attached Files
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    08-10-2007
    Posts
    51
    Here is the complete solution without specifying your start and end sheets:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-17-2008
    Location
    Moon
    Posts
    8
    Wow, that's pretty easy. I feel dumb.

    Especially because I can't get the CountIF statement to work that I replaced Count with.

    I get a #VALUE error. My goal is to count the number of times "Off" appears in that cell, but it's not as straight forward as I had hoped.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    07-17-2008
    Location
    Moon
    Posts
    8
    Quote Originally Posted by piyushdabomb

    Please Login or Register  to view this content.
    I understand all of the script, except what is quoted above...

    what is r1.Formula doing?

    Thanks!

  6. #6
    Registered User
    Join Date
    07-17-2008
    Location
    Moon
    Posts
    8
    One more thing, the function is writing on the summary tab, not the Temp_Sheet that is added. I tried to select:
    Please Login or Register  to view this content.
    and Activate
    Please Login or Register  to view this content.
    the Temp_Sheet for writing, but that didn't work.
    Last edited by VBA Noob; 07-17-2008 at 03:45 PM.

  7. #7
    Registered User
    Join Date
    08-10-2007
    Posts
    51
    The code should work. The Temp_Sheet tab will not work because I create it and then delete it because that temporary sheet is used to get you the average value spit out from a collection.

    The statement:

    r1.Formula = "=round(average(A1:A" & r1.Offset(-1, 0).row & "),2)"

    essentially states: "In the temp_sheet tab, get the values from range A1 to the last item in the temporary sheet where all my values are pasted".

    r1.offset(-1,0).row takes the row number of the data point right before you enter the formla.

  8. #8
    Registered User
    Join Date
    07-17-2008
    Location
    Moon
    Posts
    8
    I'm using Excel 2007 (would that make a difference?). The calculations are being written in the summary tab, not on the temp_sheet tab, see attached.

    I've added the code you provided to this workbook. it should be under the VBScript section of excel. I put the curser on the first line and hit F5. That's when it provides the results on the summary tab. Maybe it's operator error?

    Thanks
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    07-17-2008
    Location
    Moon
    Posts
    8
    Ok, I figured it out. I added:
    Set r1 = Worksheets("Temp_Sheet").Range("A1")

    which made it write on Temp_Sheet instead of the summary tab.

    I've attached a new spreadsheet. This is the actual one I'm using. There are 32 floors that I'm trying to get an average of. using the code provided, I did a VERY inefficient method to compute averages. It's ugly and I don't like it. There's got to be a better way to do this, but I've never used VB before this week. Please don't laugh...

    Please Login or Register  to view this content.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    08-10-2007
    Posts
    51
    Hehe. Apologies for not getting back to you earlier. Do you still need assistance?

+ 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