我试图将值读入子函数,运行条件循环,然后在工作表中打印输出值。但是,我遇到了一些问题。在子函数infiltPrint中,我似乎无法获取要在单元格中打印的值。
Option Explicit
Dim dz() As Double
Dim WC() As Double
Dim fc() As Double
Dim NL, i As Integer
Dim sumdrain As Double
Dim infl As Double
Sub main()
Call InfiltRead
Call infilt
Call Infiltprint
End Sub
Sub InfiltRead()
Dim dz() As Double
Dim WC() As Double
Dim fc() As Double
Dim NL, i As Integer
Dim sumdrain As Double
sumdrain = 0
Dim infl As Double
'read inputs
NL = 10
infl = Cells(2, 1)
Application.ScreenUpdating = False
Worksheets("Sheet1").Activate
ReDim dz(NL)
ReDim WC(NL)
ReDim fc(NL)
For i = 1 To NL
dz(i) = Cells(1 + i, 3)
WC(i) = Cells(1 + i, 7)
fc(i) = Cells(1 + i, 5)
Next i
End Sub
Sub infilt()
Dim j As Integer
j = 1
While (infl > 0) And (j <= NL)
If infl > (fc(i) - WC(j)) * dz(j) Then
infl = infl - (fc(i) - WC(j)) * dz(j)
WC(j) = fc(i)
Else
WC(j) = WC(j) + infl / dz(j): infl = 0
End If
j = j + 1
Wend
If infl > 0 Then
sumdrain = sumdrain + infl
infl = 0
End If
End Sub
Sub Infiltprint()
Dim col As Double
Dim rw As Double
col = 7
For rw = 2 To 11
Cells(rw + 1, col).Value = WC()
Next rw
End Sub
我在“Cells(Row + 1,col)= WC()”
行中不断出现类型不匹配我知道这可能不是我代码中唯一的错误。坦率地说,我不知道如何从infilt sub中打印我的值。我试图使用子主...
答案 0 :(得分:1)
将打印以下版本的代码。它改变了下面提到的几件事。
您遇到的主要问题是将WC阵列复制到工作表中。您试图将整个阵列分配给单个单元格。
修复是将整个WC阵列分配给你想要放入的完整单元格。为了实现这一点,WC必须进行转置。通常需要将WC标注为Variant(&#34; Dim WC As Variant&#34;)以将数组分配给范围,新代码反映了该范围。
我还清除了一些重新声明的变量,并改变了对三个数组进行索引的方式,以便将数组的下限和上限与您的计数器对齐。
在intfilt()子句中,你使用i作为循环中的索引,但没有初始化或迭代它,并且也发生了变化。
cpearson.com是一个组织良好且深入的VBA编码资源 - 包括使用数组。你可能想看一下。
Option Explicit
Dim dz() As Double
Dim WC As Variant
Dim fc() As Double
Dim NL As Long, i As Long '<- Integer gets converted to Long internally, so do it directly
'<- Way you did it results in Variant type for i
Dim sumdrain As Double
Dim infl As Double
Sub main()
InfiltRead <- "Call" not needed
infilt
Infiltprint
End Sub
Sub InfiltRead()
sumdrain = 0 '<- variables in this sub are already declared, don't redeclare
'read inputs
NL = 10
infl = Cells(2, 1).Value
Application.ScreenUpdating = False ' <- if processing lots of data, would want to turn off auto sheet recalculation as well
Worksheets("Sheet1").Activate
ReDim dz(1 To NL) '<- set lower bound to avoid default value of 0
ReDim WC(1 To NL) '
ReDim fc(1 To NL)
For i = 1 To NL
dz(i) = Cells(1 + i, 3).Value '<- could assign each range directy to an array, but this ok
WC(i) = Cells(1 + i, 7).Value
fc(i) = Cells(1 + i, 5).Value
Next i
Application.ScreenUpdating = True '<- cleanup code
End Sub
Sub infilt()
Dim j As Long
i = 1 '<- DEBUG placeholder - you used i in this sub, but did not initialize or iterate it
j = 1
While (infl > 0) And (j <= NL)
If infl > (fc(i) - WC(j)) * dz(j) Then
infl = infl - (fc(i) - WC(j)) * dz(j)
WC(j) = fc(i)
Else
WC(j) = WC(j) + infl / dz(j)
infl = 0
End If
j = j + 1
i = i + 1 '<- DEBUG placeholder iteration
Wend
If infl > 0 Then
sumdrain = sumdrain + infl
infl = 0
End If
End Sub
Sub Infiltprint()
Dim col As Long
Dim rw As Long
rw = 2
col = 9 '<- Changed from 7 to 9 for debugging (so original values would not be overwritten)
Range(Cells(rw, col), Cells(rw + NL - 1, col)).Value = _
Application.Transpose(WC) '<- needed to get proper references for assignment
End Sub