Results 1 to 2 of 2

Thread: [RESOLVED] Inconsistent Output

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Resolved [RESOLVED] Inconsistent Output

    I wrote a rather simple method that, with a rather large dataset consisting of climate data for a specific region, calculates the wettest year (year with the most rainfall). Rainfall is one column of many, which has a value for every day, of every month, of every year for about 25 years worth of data. (Each day has its own row).

    Anyway, the below code always determines the correct year, except the amount of rain is not always right. When run first the number is correct, but after running again (or other methods which do sort the data differently, but don't change any values) the value is incorrect. The only things I can think that are affecting the code is incorrect sorting or incorrect rounding errors. Any help? Am I too confusing? Thanks

    vb Code:
    1. Sub findWettestYear()
    2. Sheets("CLIMLONG").Activate
    3. lastRow = Sheets("CLIMLONG").UsedRange.Rows.Count
    4. Dim startRange As Range
    5. Set startRange = Worksheets("CLIMLONG").Range("A2:N" & lastRow)
    6.  
    7. Application.ScreenUpdating = False
    8.  
    9. With startRange
    10.         .Sort Key1:=Range("E2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
    11.         False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    12. End With
    13.  
    14. Application.ScreenUpdating = True
    15.  
    16. Dim tempRain As Double
    17. tempRain = 0
    18. Dim tempYear As Integer
    19. tempYear = 0
    20.  
    21. Dim highRain As Double
    22. highRain = -1
    23. Dim highYear As Integer
    24. highYear = 0
    25.  
    26. Dim y As Integer
    27. y = 0
    28.  
    29. For x = 2 To lastRow
    30.     tempYear = Sheets("CLIMLONG").Cells(x, 5)
    31.     tempRain = 0
    32.     y = x
    33.     While tempYear = Sheets("CLIMLONG").Cells(y, 5)
    34.         tempRain = tempRain + Sheets("CLIMLONG").Cells(y, 6).Value
    35.         y = y + 1
    36.     Wend
    37.     If tempRain > highRain Then
    38.         highRain = tempRain
    39.         highYear = tempYear
    40.     End If
    41.     x = y + 1
    42. Next x
    43.    
    44. If highYear < 100 Then 'makes 97 into 1997
    45.     highYear = highYear + 1900
    46. End If
    47.  
    48. lbl4.Caption = "Wettest Year is " & highYear & "."
    49. lbl5.Caption = "With " & Format(highRain, "Standard") & " Inches of Rainfall."
    50. End Sub

  2. #2

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Inconsistent Output

    hah, stupid mistake... My initialization of tempRain was to 0 (the int) not 0.0 (the double), so some rounding errors occurred...

    fixed line was


    Dim tempRain As Double
    tempRain = 0#

Posting Permissions

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



Click Here to Expand Forum to Full Width