我正在尝试处理条件循环。一切似乎都在起作用,但它并没有进入下一个和下一个j并继续循环。它似乎在循环的一个循环之后结束。我一直在研究这个问题,对VBA来说非常新。
Sub Read()
NumMonth = 12
Worksheets("Sheet1").Activate
ReDim Month(1 To NumMonth)
ReDim WCinit(1 To NumMonth)
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
WCinit(i) = Cells(4 + i, 10).Value
Precip(i) = Cells(4 + i, 2).Value
RefET(i) = Cells(4 + i, 3).Value
Next i
For j = 1 To NumMonth + 1
WC(j) = Cells(3 + i, 11).Value
Next j
Application.ScreenUpdating = True
End Sub
Sub Test()
Dim fc As Double
fc = 0.3
NumMonth = 12
i = 1
j = 2
Dim pwp As Double
pwp=0.1
Dim dz As Double
dz = 0.5 'm
Do
If WC(j - 1) + WCinit(i) > pwp And fc - (WC(j - 1) + WCinit(i)) + RefET(i) < Precip(i) Then
Runoff(i) = (Precip(i) - (fc - (WC(j - 1) + WCinit(i)) + RefET(i))) * 0.5
Percolation(i) = (Precip(i) - (fc - (WC(j - 1) + WCinit(i)) + RefET(i))) * 0.5
WC(j) = fc
ElseIf WC(j - 1) + WCinit(i) > pwp And fc - (WC(j - 1) + WCinit(i)) + RefET(i) < Precip(i) Then
Runoff(i) = 0
Percolation(i) = 0
WC(j) = WC(j - 1) + WCinit(i) + Precip(i) - RefET(i)
Else
WC(j) = pwp
End If
j = j + 1
i = i + 1
Loop While j<13
答案 0 :(得分:2)
我已经对你提供的代码做了一些工作(如下面的注释所示),主要需要仔细调试程序的逻辑。您仍然会收到编译器错误,因为代码中没有分配两个数组,而且我没有足够的信息来正确执行此操作。
VBA中用于驯服顽固代码的最佳资源是内置调试器。有了它,就可以逐行查看代码,了解每个变量在每一步的变化情况。
如果您不熟悉调试器,可以在线获得一些很好的解释,例如Chip Pearson的explanation of the basics。
可以使循环和索引更容易处理的一个变化是将“第13个月”值放入与数组分开的变量中,这将是数据的12个月简单集合。
这将是一种减少另外需要的混乱簿记的方法(“程序中此时的索引应该是j,还是j-1,或j + 1?”)。我不知道这种方法是否真的适合您的数据和计算。
Option Explicit ' Require declaration of all variables
Private Const NumMonth As Long = 12 ' Make these variable available to all subroutines
Private month As Variant ' and functions in the module. Needed because they
Private WCinit As Variant ' would otherwise be out of scope in the Test()
Private WC As Variant ' subroutine.
Private Precip As Variant ' The arrays need to be declared as type Variant,
Private RefET As Variant ' so we can make the assignment
Private Percolation As Variant
Private Runoff As Variant
Sub Read()
Dim firstMoDataRow As Long
Dim lastMoDataRow As Long
firstMoDataRow = 5
lastMoDataRow = 16
' if you want to avoid using the transpose function in the following
' array assignments, you will need to reference the arrays as
' two-dimensional, e.g., WC(i,1) or WC(3,1), etc., with the
' second index always 1 (that's what many often do).
month = WorksheetFunction.Transpose(Range(Cells(firstMoDataRow, 1), Cells(lastMoDataRow, 1)).Value) ' Direct assignment of values
WCinit = WorksheetFunction.Transpose(Range(Cells(firstMoDataRow, 10), Cells(lastMoDataRow, 10)).Value) ' in the worksheet ranges
WC = WorksheetFunction.Transpose(Range(Cells(firstMoDataRow - 1, 11), Cells(lastMoDataRow, 11)).Value) ' to the arrays.
Precip = WorksheetFunction.Transpose(Range(Cells(firstMoDataRow, 2), Cells(lastMoDataRow, 2)).Value)
RefET = WorksheetFunction.Transpose(Range(Cells(firstMoDataRow, 3), Cells(lastMoDataRow, 3)).Value)
' ?! Percolation and runoff are not assigned in your code, so I don't know
' the correct row and column references in the worksheet.
End Sub
Sub Test()
Dim fc As Double
Dim pwp As Double
Dim dz As Double
Dim i as Long, j as Long
fc = 0.3
pwp = 0.1
dz = 0.5 'm
i = 1
j = 2
Do While i <= NumMonth And j <= NumMonth + 1 ' I am assuming here that you will sort out
' the correct initializing and incrementing
' of the loop indexes, which I have not
' puzzled out
If WC(j - 1) + WCinit(i) > pwp And fc - (WC(j - 1) + WCinit(i)) + RefET(i) < Precip(i) Then
Runoff(i) = (Precip(i) - (fc - (WC(j - 1) + WCinit(i)) + RefET(i))) * 0.5
Percolation(i) = (Precip(i) - (fc - (WC(j - 1) + WCinit(i)) + RefET(i))) * 0.5
WC(j) = fc
ElseIf WC(j - 1) + WCinit(i) > pwp And fc - (WC(j - 1) + WCinit(i)) + RefET(i) < Precip(i) Then
' You are getting a "Subscript out of range" error here because Runoff() and
' Percolation<> did not get assigned in the Read sub
Runoff(i) = 0
Percolation(i) = 0
WC(j) = WC(j - 1) + WCinit(i) + Precip(i) - RefET(i)
Else
WC(j) = pwp
End If
j = j + 1
i = i + 1
Loop
End Sub