我正在尝试读取 WC(1) 的单个值。然后我想在条件循环中使用 WC(j) 的初始值。我让 WC(j) 的第一个值顺利运行(它满足循环中的第一个条件),但是,第一个条件应该建立 WC(j) 数组中的下一个值。但是,它没有为数组建立一个值,它只是说为空。特别是在这些行中: WC(j) = fc WCinit(j) = WC(j - 1)
当我单步执行代码时,它会说 fc=0.3 但 WC(j) 是空的,即使我已将它们设置为彼此相等。由于 WC(j) 值为空,它只会满足我循环的“else”条件。
下一行代码也有同样的问题,WC(j-1) 有一个值,它被设置为等于 WCinit(j),但 WCinit(j) 保持为空。
Dim Month() As Double
Dim WC() As Variant
Dim WCinit() As Variant
Dim NumMonth As Long, i As Long, j As Long
Dim Precip() As Double
Dim RefET() As Double
Dim Runoff() As Double
Dim Percolation() As Double
Sub main()
WaterBalanceReadMediterranean
WaterBalanceMediterranean
WaterBalanceReadPlains
WaterBalancePlains
End Sub
Sub WaterBalanceReadMediterranean()
NumMonth = 12
ReDim Month(1 To NumMonth)
ReDim WCinit(1 To NumMonth + 1)
ReDim WC(1 To NumMonth + 1)
ReDim Precip(1 To NumMonth)
ReDim RefET(1 To NumMonth)
ReDim Percolation(1 To NumMonth + 1)
ReDim Runoff(1 To NumMonth + 1)
For i = 1 To NumMonth
Month(i) = Cells(4 + i, 1).Value
Precip(i) = Cells(4 + i, 2).Value
RefET(i) = Cells(4 + i, 3).Value
Next i
For j = 1 To 1
WC(j) = Cells(3 + j, 11).Value
Next j
Application.ScreenUpdating = True
End Sub
Sub WaterBalanceMediterranean()
Dim fc As Double
fc = Cells(4, 7).Value
NumMonth = 12
i = 1
j = 2
Dim pwp,dz As Double
Do
If WC(j) >= pwp And (fc - WC(j - 1) + RefET(i)) < Precip(i) Then
Runoff(i) = (Precip(i) - (fc - WC(j - 1) + RefET(i))) * 0.5
Percolation(i) = (Precip(i) - (fc - WC(j - 1) + RefET(i))) * 0.5
WC(j) = fc
WCinit(j) = WC(j - 1)
ElseIf WC(j) >= pwp And (fc - WC(j - 1) + RefET(i)) > Precip(i) Then
Runoff(i) = 0
Percolation(i) = 0
WC(j) = WC(j - 1) + Precip(i) - RefET(i)
WCinit(j) = WC(j - 1)
Else
Runoff(i) = 0
Percolation(i) = 0
WC(j) = pwp
WCinit(j) = WC(j - 1)
End If
j = j + 1
i = i + 1
Loop While j < 14
End Sub