[英]VBA: Problem with passing arguments to userform
I have searched and tried for 6 hours now but I can't solve the problem.我已经搜索并尝试了 6 个小时,但我无法解决问题。
My idea: I want to show a userform in Excel-VBA with some radiobuttons, one listelement and one textfield.我的想法:我想在 Excel-VBA 中显示一个用户表单,其中包含一些单选按钮、一个列表元素和一个文本字段。 I want to pass some arguments to the userform to work with it because I don't want to calculate inside the userform-code (which works) I don't want to declare global variables as well.
我想将一些 arguments 传递给用户表单以使用它,因为我不想在用户表单代码(有效)内部进行计算,我也不想声明全局变量。
I tried the following which worked:我尝试了以下方法:
calculate directly in the userform直接在用户表单中计算
global variables全局变量
This hasn't worked:这没有奏效:
declared userform as variable and worked with the variable将 userform 声明为变量并使用该变量
every possible combination of [Public|Private|Friend] Property Let (ByRef|ByVal)
[Public|Private|Friend] Property Let (ByRef|ByVal)
的所有可能组合
Property Set
doesn't work in any case Property Set
在任何情况下都不起作用
The code from my main (module)我的主要(模块)中的代码
Option Explicit
Sub main()
With New usr_mainInput
.counter = 3
.Show
End With
End Sub
My code in the userform我在用户表单中的代码
Option Explicit
Private miCounter As Integer
Property Get counter() As Integer
counter = miCounter
End Property
Property Let counter(c As Integer)
Set miCounter = c
End Property
Private Sub userform_initialize()
Dim i As Integer
For i = 1 To counter 'miCounter don't work as well
Debug.Print i
Next i
End Sub
Private Sub btn_ok_Click()
Me.Hide
End Sub
Object variable or With block variable not set Object 变量或未设置块变量
If it comes to the Property Let counter()
it drops a compile error:如果涉及到
Property Let counter()
它会抛出一个编译错误:
Object required (Error 424)
需要 Object(错误 424)
Property Let counter(c As Integer) Set miCounter = c End Property
An object is required , only because of the Set
keyword.一个object 是必需的,只是因为
Set
关键字。 This isn't a reference assignment, it's a value assignment.这不是引用赋值,而是值赋值。
View it like this:像这样查看它:
Property Let counter(c As Integer)
Let miCounter = c
End Property
Just don't actually type up the Let
keyword (it would work though), it's obsolete :)只是不要真正输入
Let
关键字(虽然它会起作用),它已经过时了:)
Also note that the implicit modifier for Property Let/Set
procedure argument, is always Byval
- this is different than anything else in VBA, where thr implicit modifier is ByRef
;另请注意,
Property Let/Set
过程参数的隐式修饰符始终为 Byval - 这与Byval
中的其他任何内容不同,其中隐式修饰符为ByRef
; consider making the ByVal
modifier explicit.考虑使
ByVal
修饰符显式。
Private Sub userform_initialize() Dim i As Integer For i = 1 To counter 'miCounter don't work as well Debug.Print i Next i End Sub
That loop will never iterate anything, because the Initialize
handler runs here:该循环将永远不会迭代任何内容,因为
Initialize
处理程序在这里运行:
With New usr_mainInput
I mean, it runs when the New usr_mainInput
instruction returns, but before the object reference is given to the With
block (note that this is true for any class, not just forms) - that's well before the .counter = 3
assignment, Rule of thumb, you want to initialize instance state in that handler, not consume it.我的意思是,它在
New usr_mainInput
指令返回时运行,但在 object 引用被提供给With
块之前(请注意,这对于任何 class 都是如此,而不仅仅是表单) - 这远远早于.counter = 3
分配,规则拇指,您想在该处理程序中初始化实例 state,而不是使用它。
Consider using the Activate
handler instead.请考虑改用
Activate
处理程序。 That one will run immediately after the .Show
call.那个将在
.Show
调用之后立即运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.