今天,以下代码对我不起作用。我认为有人在篡改我的电脑。我想知道,这怎么可能被复制,或者我的代码有错误:
'number of rows for a certain column:
dim c as integer
c = WorksheetFunction.counta(columns("A:A"))
'get a value
c = range("A1")
额外的:
我的代码是家庭作业,“蒙特卡洛模拟”。它基本上改变了用于烘焙饼干的物品清单的成本,并使用不同类型的分布(均匀、正常、离散)来做到这一点。由于带有循环引用的格式错误的循环,Excel 耗尽了它的资源 - 所以错误消息)。我反复重新启动 Excel。最终,我使用了教授提供的原始代码,并逐步重新插入了我已经编写过的函数和程序,以了解我的编码工作是如何导致 Excel 错误消息的。我在 1 分钟内完成了 1000 个“蒙特卡罗模拟”,而不是之前观察到的 30 分钟。我提供的代码我认为是基本的,但我无法解决问题,即使我将旧代码复制到我的 VBA 模块中。
2021 年 7 月 20 日添加
此代码使用控件集合来获取表单上数据的值。您需要具有两个文本字段的表单,这些文本字段应该具有双值内容。因此,从今天开始,控件集合不起作用。
即txtPerc1
,txt1
然后该函数将返回一个值以及离散分布。
控件行被注释掉,因为我收到错误“未定义子或函数。我检查了字段的名称,没关系。
'sample call:
'discreteDistribtion("Hallo", 3)
' So I have six fields named:
' halloPerc1, halloPerc2, halloPerc3 and hallo1, hallo2, and hallo3
' the function takes here "hallo" and generates the names Hallo1, Hallo2, Hallo3 and halloPerc1, halloPerc2, and halloPerc3
' the function takes the value of hallo1, hallo2, or hallo3, each of these fields having doubles as content.
' According to the logic, hallo1, hallo2 or hallo3 values are returned!
' The code does not work on my computer, when I uncomment the controls code, it worked the weeks before.
Function discreteDistribution(strCtrl As String, iEnd As Integer, Optional iStart As Integer = 1)
Dim i As Integer, sum As Double, r As Double, strTemp As String
sum = 0
r = Rnd
discreteDistribution = 0
For i = iStart To iEnd
strTemp = strCtrl & "Perc" & i
''''''sum = sum + Controls(strTemp).Value
If r < sum Then
strTemp = strCtrl & i
'''''' discreteDistribution = Controls(strTemp)
Exit For
End If
Next i
End Function
我在单独的工作表上向我的工作簿添加了第二个用户表单,并且以下代码正在运行:
Option Explicit
Dim i As Integer
Private Sub CommandButton1_Click()
For i = 1 To 10
Controls("TextBox" & i).Value = Cells(i + 1, 1).Value
Next i
End Sub
Private Sub UserForm_Click()
End Sub
这段代码用电子表格上收集的电话号码填充了一堆文本框。尽管如此,我还是无法让我的原始代码工作。