vba - 在 vba 中传递表单信息
问题描述
我有一个子,我在其中定义了 2 个变量,并将这些字符串中的 1 个分配给我已经创建的表单中的文本框。
Sub test()
dim x as string
dim y as string
x = "hello"
y = "friends"
myForm.textbox1.value = x
myForm.show
End sub
现在表单处于活动状态,并且表单正在征求信息,并根据用户输入执行其他代码,我记得我的字符串y
是相关的。但我不需要它在文本框中。这是我开始怀疑的地方:有没有什么方法可以将数据传递给表单,以便表单“可以看到”来自 sub 的活动,test()
而无需将其分配给表单上的对象?
解决方案
或者您可以使用 UserForm 对象的 Tag 属性
Sub test()
dim x as string
dim y as string
x = "hello"
y = "friends"
With myForm
.Tag = y ‘ “pass” the value of y to the form by storing it into its Tag property
.textbox1.value = x
.show
y = .Tag ‘ assign y the value stored in the form Tag property
End With
End sub
这样,在您的 UserForm 代码中,您可以按如下方式检索该值:
Dim y As String ‘ being inside the form this is a locally scoped variable
y= Me.Tag ‘ assign “local” y the value stored in Tag property from outside the form
.... ‘ your code
Me.Tag = y ‘ store final value of “local” y to Tag property and make it avalable to the “outer” world
推荐阅读
- snowflake-cloud-data-platform - Snowflake EXPLAIN 查询的输出中不存在 TableScan、InnerJoin、JoinFilter 等某些操作
- linux - 循环遍历bash中的两个目录
- mysql - 如何计算从上一个日期到更新的值
- oracle - 如何在oracle sql developer中将特定列从一个表复制到另一个表?
- unicode - 为简体中文标识符构建令牌
- docker - 如何从现有的 Kubernetes 清单构建 Helm 图表
- python - 在 Linux 中启动 Jupyterlab
- python - 简单的 Flask 显示空白页
- python - 使用 Big (O) 进行时间复杂度分析
- arrays - Excel/Sheets 结合行列内容并找到尊重的价值