|
-
Jul 26th, 2007, 12:34 PM
#1
Thread Starter
Hyperactive Member
[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:
Sub findWettestYear()
Sheets("CLIMLONG").Activate
lastRow = Sheets("CLIMLONG").UsedRange.Rows.Count
Dim startRange As Range
Set startRange = Worksheets("CLIMLONG").Range("A2:N" & lastRow)
Application.ScreenUpdating = False
With startRange
.Sort Key1:=Range("E2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With
Application.ScreenUpdating = True
Dim tempRain As Double
tempRain = 0
Dim tempYear As Integer
tempYear = 0
Dim highRain As Double
highRain = -1
Dim highYear As Integer
highYear = 0
Dim y As Integer
y = 0
For x = 2 To lastRow
tempYear = Sheets("CLIMLONG").Cells(x, 5)
tempRain = 0
y = x
While tempYear = Sheets("CLIMLONG").Cells(y, 5)
tempRain = tempRain + Sheets("CLIMLONG").Cells(y, 6).Value
y = y + 1
Wend
If tempRain > highRain Then
highRain = tempRain
highYear = tempYear
End If
x = y + 1
Next x
If highYear < 100 Then 'makes 97 into 1997
highYear = highYear + 1900
End If
lbl4.Caption = "Wettest Year is " & highYear & "."
lbl5.Caption = "With " & Format(highRain, "Standard") & " Inches of Rainfall."
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|