我是 VBA 新手,正在尝试编写一个简单的宏。我在下面粘贴了我的代码。基本上,我在单元格 B5 到 B15 中有 11 个随机数。如果这个数字小于 0.65,我希望它在旁边的列中打印 TRUE。如果它大于 0.65,我希望它在旁边的列中打印 FALSE。我想我基本上已经把代码写下来了,但是 Sub PrintandRead 中的“Next t”,t 的值将是 5 到 15(这是正确的)但是它会上升到 markov 函数,其中 t 然后返回 t=0 . 为什么它没有将“Next t”值带入函数?
Option Explicit
Function markov(pwd As Double, pww As Double) As Boolean
Static wetYesterday As Boolean
pwd = 0.4
pww = 0.65
Dim c As Double
Dim t As Double
If wetYesterday Then c = t - pww Else c = t - pwd
If c <= 0 Then
wetYesterday = True: markov = True
Else
wetYesterday = False: markov = False
End If
End Function
Sub ReadAndPrint()
Dim t As Double
Dim p As Double
Dim z(11) As Double
Application.ScreenUpdating = False
Worksheets("Sheet1").Activate
p = 2
For t = 5 To 15
z(t - 4) = Cells(t, p)
Next t
p = p + 1
For t = 5 To 15
Cells(t, p) = markov(0.4, 0.65)
'z(t - 4)
Next t
End Sub