全局变量在用户窗体中 [英] Global Variable in Userform

查看:165
本文介绍了全局变量在用户窗体中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个用户表单。



在第一个例子中,我给了一个名为Word的变量的值。

在第二个中,我有一个标签,我需要标题成为可变的Word。



示例:

  String 

Private Sub Userform1_Activate
Word =Today is Saturday
End Sub

Private Sub Userform2_Activate
Label1.Caption = Word
End Sub

但这不起作用。标签标题的值为零。
有人可以帮我吗?
$ b

谢谢。 c>第一种形式

私人小组CommandButton5_Click()

Db =C:\Users\Desktop\db.txt

设置File1 = CreateObject(Scripting.FileSystemObject)
设置File2 = File1.OpenTextFile(Db,1)

直到File2.AtEndOfStream

If(File2 .Readline = TextBox1)Then Exit
If File2.AtEndOfStream Then WordNotFound.Show
If File2.AtEndOfStream Then TextBox1.Value =
If File2.AtEndOfStream Then Exit Sub

Loop

Word = File2.Readline

MsgBox Word

TextBox1.Value =

End Sub

第二个表格

  Private Sub UserForm_Click()

Label1.Caption = Word

End Sub


解决方案

正如我在评论中所说的那样,您的方法应该可行。这里是我试过的测试代码

1- In Module1

 公共词作为字符串



<2->创建2用户表单 - UserForm1 UserForm2



<在UserForm1中

$ $ $ $ pre $ Private $ UserForm_Activate(
Word =This is Saturday
End Sub



2b-在UserForm2中

  Private Sub UserForm_Activate()
Label1.Caption = Word
End Sub



<3->然后在 ThisWorkbook

  Private Sub Workbook_Open )
UserForm1.Show
UserForm2.Show
End Sub

所以当你关闭UserForm1时,UserForm2会显示如下




I search about this in the forum and found some answers but did not work for me.

I have two UserForms.

In the first one, I give a value to a variable called Word.

In the second one, I have a Label that I need the caption to become the variable Word.

Example:

Public Word as String

Private Sub Userform1_Activate
   Word = "Today Is Saturday"
End Sub

Private Sub Userform2_Activate
   Label1.Caption = Word
End Sub

But this does not work. The Label caption gets Zero for value. Could anybody help me on this?

Thanks.

First Form

Private Sub CommandButton5_Click()

Db = "C:\Users\Desktop\db.txt"

Set File1 = CreateObject("Scripting.FileSystemObject")
Set File2 = File1.OpenTextFile(Db, 1)

Do Until File2.AtEndOfStream

If (File2.Readline = TextBox1) Then Exit Do
If File2.AtEndOfStream Then WordNotFound.Show
If File2.AtEndOfStream Then TextBox1.Value = ""
If File2.AtEndOfStream Then Exit Sub

Loop

Word = File2.Readline

MsgBox Word

TextBox1.Value = ""

End Sub

Second Form

Private Sub UserForm_Click()

Label1.Caption = Word

End Sub

解决方案

As I said in my comment, that your method should work. Here is the test code that I tried

1- In Module1

Public Word As String

2- Create 2 user forms - UserForm1 and UserForm2

2a- In UserForm1

Private Sub UserForm_Activate()
    Word = "This is Saturday"
End Sub

2b- In UserForm2

Private Sub UserForm_Activate()
    Label1.Caption = Word
End Sub

3- Then in ThisWorkbook

Private Sub Workbook_Open()
    UserForm1.Show
    UserForm2.Show
End Sub

So when you close UserForm1, the UserForm2 would be displayed as below

这篇关于全局变量在用户窗体中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆