Excel VBA:读取10行数据,运行条件循环,并打印输出值

Excel VBA:读取10行数据,运行条件循环,并打印输出值,excel,vba,Excel,Vba,我试图将值读入子函数,运行条件循环,然后在工作表中打印输出值。然而,我遇到了一些问题。在子函数infittprint中,我似乎无法获取要在单元格中打印的值 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

我试图将值读入子函数,运行条件循环,然后在工作表中打印输出值。然而,我遇到了一些问题。在子函数infittprint中,我似乎无法获取要在单元格中打印的值

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
选项显式
Dim dz()为双精度
Dim WC()为双精度
Dim fc()为双精度
Dim NL,i作为整数
暗集水坑为双集水坑
暗infl为双精度
副标题()
呼叫加密胎面
呼叫填充
调用Infiltprint
端接头
附属法例(
Dim dz()为双精度
Dim WC()为双精度
Dim fc()为双精度
Dim NL,i作为整数
暗集水坑为双集水坑
集水沟=0
暗infl为双精度
'读取输入
NL=10
infl=单元格(2,1)
Application.ScreenUpdating=False
工作表(“表1”)。激活
雷迪姆dz(荷兰)
雷迪姆WC(NL)
雷迪姆fc(NL)
对于i=1到NL
dz(i)=单元(1+i,3)
WC(i)=单元(1+i,7)
fc(i)=单元(1+i,5)
接下来我
端接头
子填充()
作为整数的Dim j
j=1
而(infl>0)和(j(fc(i)-WC(j))*dz(j)则
infl=infl-(fc(i)-WC(j))*dz(j)
WC(j)=fc(i)
其他的
WC(j)=WC(j)+infl/dz(j):infl=0
如果结束
j=j+1
温德
如果infl>0,则
集水沟=集水沟+infl
infl=0
如果结束
端接头
子填充点()
双色
将rw调暗为双色
col=7
对于rw=2到11
单元格(rw+1,列).Value=WC()
下一个rw
端接头
我一直在“Cells(Row+1,col)=WC()行”中发现类型不匹配


我知道这可能不是我代码中唯一的错误。坦率地说,我不知道如何从填充sub打印我的值。我尝试使用sub-main…

以下版本的代码将被打印。它改变了以下注意事项

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

解决方法是将整个WC数组分配给您想要放入的完整单元格集。要使其工作,WC必须进行转置。通常需要将WC作为变量进行尺寸标注(“Dim WC as Variant”)以将数组分配给范围,新代码反映了这一点

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

在intfilt()子函数中,您将i用作循环中的索引,但没有对其进行初始化或迭代,这也发生了变化

是一个组织良好且深入的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
选项显式
Dim dz()为双精度
Dim-WC作为变体
Dim fc()为双精度

Dim NL As Long,i As Long’首先,您正在尝试将数组分配给单元格,即WC()而不是WC(索引):(我如何解决此问题?WC(1)?然后如何为下一个WC值前进到下一个单元格?有一种更好的方法将数组分配给某个范围:在:使用单元格(行+列)中对我的上一条注释进行编辑时很慢).Value=WC,不带循环。需要“Value”。可能需要将名称“Row”更改为其他名称,因为它是一个关键字。我假设WC仍在范围内。我按照您的建议对代码进行了所有更改,但在同一行代码中仍然出现相同的类型不匹配错误。上面的代码似乎没有打印WC(j)正确。它只是在第9列中重新打印fc(i)。如果您还没有,您应该对一些数据行进行代数运算,以检查您期望的WC(j)是什么。我在虚拟参数和数据上测试了代码;打印的内容根据这些值的设置而有所不同。