Excel 的问题 - 第三方是否篡改了我的计算机?

问题描述

以下代码今天对我不起作用。我认为有人在篡改我的电脑。我想知道,这怎么可能被复制,或者我的代码有错误:

'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 日添加
此代码使用控件集合来获取表单上的数据值。您需要带有两个文本字段的表单,它们应该具有双值内容。因此,从今天起,控件集合不起作用。

txtPerc1txt1
然后函数将返回一个值以及离散分布。
控件行被注释掉,因为我收到错误“子或函数未定义。我检查了字段的名称,没问题。

'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,and halloPerc3
' the function takes the value of hallo1,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

此代码用电子表格上收集的电话号码填充一堆文本框。尽管如此,我还是无法让我的原始代码正常工作。

Excel 识别出错误的代码,突出显示了上图所示错误的位置:

解决方法

虽然这里没有回答全部问题,但我今天的问题已经解决了。控件集合是用户表单的一个属性。所以为了从模块内部调用这个集合,它需要知道它所关联的表单。所以不是调用 controls("Name of Control") = ... 调用必须是: form.controls("Name of Control") 然后代码工作.

感谢所有回复我鼓励请求的人。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...