+ Reply to Thread
Results 1 to 3 of 3

Isempty problems

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2012
    Location
    Stamford, CT
    MS-Off Ver
    Excel 2003
    Posts
    24

    Isempty problems

    Hi all , I wrote code that works just fine, however, I realized there were parts of the data I was using that had 0's in it for holiday.

    Bottom line, this is code that calculated the weighted average of prices for 12 months (12 rows across) and Drows across. I'm trying to use the "do while is empty" approach but can't get the syntax to work. Here's the code

    For r = 1 To drows
     
    
        Totwavg = 0
        Tothours = 0
        priceavg = 0
                
             For n = 1 To ncontracts
                 Do While IsEmpty(rngs.Cells(r, n + 1))
                      If r > drows Then Exit Do
                        Hours = Application.VLookup(PWR.Cells(1, n + 1).Value, vHours, 1 + OnOff1, False)
                        priceavg = PWR.Cells(1 + r, n + 1).Value * Hours
                    
                         Totwavg = Totwavg + priceavg
                    
                         Hoursbucket = 0
                         Hoursbucket = Application.VLookup(PWR.Cells(1, 1 + n).Value, vHours, 1 + OnOff1, False)
                         Tothours = Tothours + Hoursbucket
                     
                       Next n
                    Worksheets("INFO").Cells(1 + r, 2).Value = Totwavg / Tothours
                End If
            Loop
    Next r

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,354

    Re: Isempty problems

    What do you mean, you "can't get the syntax to work."? Is it giving you an error? Is it running but not running correctly?

    A quick scan through the code suggests that you need to define the loops more carefully. The For n=/Next n loop begins outside of the Do While IsEmpty loop, but ends inside the Do While Loop. Or is it that the Do While loop starts inside the For n loop and ends outside the For n loop. It looks to me like the first thing to do might be to decide which of those loops is supposed to be the outer loop and which is supposed to be the inner loop and write it that way.

  3. #3
    Registered User
    Join Date
    06-25-2012
    Location
    Stamford, CT
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Isempty problems

    Thanks a lot for the info! I'm moderately new to vba so thanks for the patience, this forum has helped so much already. I have rewritten the code so I will post it tomorrow morning to see what you think. ( about 10 hours from no US eastern time) Regardless, in the mean time, any suggestions on how to by pass lines of data for periods such as christmas, easter, etc, when doing calculations such as percent differences, correlations where I use ranges of data, etc etc. Obviously I don't want to compute a standard dev with 0's in it or divide by 0 doing a calculation

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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