简体   繁体   English

从Excel单元格完成VBA用户窗体文本框

[英]Completing VBA Userform textboxes from Excel Cell

I'm trying to fill in a userform ("Userform1") textboxes ("SecurityTextBox", "VersionTextbox") from an Excel Sheet ("Version"). 我正在尝试从Excel工作表(“版本”)中填写一个用户窗体(“ Userform1”)文本框(“ SecurityTextBox”,“ VersionTextbox”)。 I've tried to look up what's going wrong but I haven't managed to figure it out. 我已经尝试查找出了什么问题,但还没有弄清楚。

Private Sub UserForm1_Initialize()
Dim ws As Worksheet
Set ws = Worksheets("Version")

SecurityTextBox.Text = ws.Cells.Range("C8").Value
VersionTextbox.Text = ws.Cells.Range("C13").Value
DeveloperTextBox.Text = ws.Cells.Range("C14").Value

End Sub

The trouble is the text boxes are just appearing blank. 问题在于文本框仅显示为空白。

The _Initialize handler is invoked when the class/form instance is created. 创建类/窗体实例时,将调用_Initialize处理程序。 If you're showing the form like this: 如果您正在显示这样的表单:

UserForm1.Show

Then it might work as expected if that's the only thing you're ever doing with the form. 然后,如果这是您使用表单所做的唯一事情,它可能会按预期工作。 Problem is, it's the form's default instance , and you don't quite control when VBA is going to initialize that global instance. 问题是,它是表单的默认实例 ,并且您无法完全控制VBA 何时初始化该全局实例。

Take control. 控制住。

With New UserForm1 'initializes a new instance of the class
    .Show
End With

Now the _Initialize handler with systematically run every time, because it's a New instance every time. 现在, _Initialize处理程序每​​次都可以系统地运行,因为它每次都是一个New实例。 See my UserForm1.Show article for more pitfalls of using forms' default instances. 查看我的UserForm1.Show文章,了解使用表单默认实例的更多陷阱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM