0

我正在尝试将值读入子函数,运行条件​​循环,然后在工作表中打印输出值。但是,我遇到了一些问题。在子函数 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()”行中不断收到类型不匹配

我知道这可能不是我的代码中唯一的错误。坦率地说,我不知道如何从渗透子打印我的值。我试图使用子主...

4

1 回答 1

1

将打印以下版本的代码。它改变了下面提到的几件事。

您遇到的主要问题是将 WC 数组复制到工作表中。您试图将整个数组分配给单个单元格。

解决方法是将整个 WC 数组分配给您想要放入的全套单元格。为此,必须转置 WC。通常需要将 WC 标注为 Variant(“Dim WC As Variant”)以将数组分配给范围,新代码反映了这一点。

我还清除了一些变量的重新声明,并更改了对三个数组进行索引的方式,以使数组的下限和上限与您的计数器对齐。

在 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
于 2013-02-15T04:31:50.247 回答