在 Excel 中使用 VBA 初始化用户窗体并加载变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10915037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:23:33  来源:igfitidea点击:

Initializing a UserForm and Loading Variables with VBA in Excel

excelvbainitialization

提问by Ehudz

I have a userform with an initialize event as follows:

我有一个带有初始化事件的用户表单,如下所示:

Public CurrSpanNum, CurrSegNum As Integer
Private Sub UserForm_Initialize()

        CurrSegNum = Sheets("UserData").Range("C2").value
        Debug.Print "SegForm, SpanNum = " & CurrSpanNum
        CurrSpanNum = Sheets("UserData").Range("D2").value
        Debug.Print "SegForm, SegNum = " & CurrSegNum

End Sub

In the immediate window I see:

在即时窗口中,我看到:

SegForm, SpanNum = 
SegForm, SegNum = 3

The value 3 is placed in both cells before the form is initialized. Why can't I seem to load SpanNum while SegNum is able to load?

在初始化表单之前,值 3 被放置在两个单元格中。为什么我似乎无法加载 SpanNum 而 SegNum 能够加载?

回答by panda-34

Well, your CurrSpanNum variable is not initialized and so prints nothing, What would you expect?

好吧,您的 CurrSpanNum 变量未初始化,因此什么也不打印,您会期望什么?