2010-04-27 251 views
86

我写了下面的代码:如何在VBA中声明全局变量?

Function find_results_idle() 

    Public iRaw As Integer 
    Public iColumn As Integer 
    iRaw = 1 
    iColumn = 1 

而我得到的错误信息:

“无效的属性在Sub或Function”

你知不知道我做错了什么?

我试图使用Global而不是Public,但遇到同样的问题。

我试图将这个函数本身声明为Public,但是这也没有好处。

我需要做什么来创建全局变量?

回答

130

您需要声明函数外部变量:

Public iRaw As Integer 
Public iColumn As Integer 

Function find_results_idle() 
    iRaw = 1 
    iColumn = 1 
29

使用全局变量,从VBA项目UI插入新模块并声明变量使用Global

Global iRaw As Integer 
Global iColumn As Integer 
87

这是一个关于问题scope

如果只想变量持续的功能寿命,使用Dim(简称尺寸)函数或子内声明的变量:

Function AddSomeNumbers() As Integer 
    Dim intA As Integer 
    Dim intB As Integer 
    intA = 2 
    intB = 3 
    AddSomeNumbers = intA + intB 
End Function 
'intA and intB are no longer available since the function ended 

一个全球变量(正如SLaks指出的)在使用Public关键字的函数之外声明。这个变量在运行应用程序的生命周期中可用。就Excel而言,这意味着只要特定的Excel工作簿处于打开状态,这些变量就可以使用。

Public intA As Integer 
Private intB As Integer 

Function AddSomeNumbers() As Integer 
    intA = 2 
    intB = 3 
    AddSomeNumbers = intA + intB 
End Function 
'intA and intB are still both available. However, because intA is public, ' 
'it can also be referenced from code in other modules. Because intB is private,' 
'it will be hidden from other modules. 

您也可以是通过与Private关键字声明它们的特定模块(或类)内只访问的变量。

如果您正在构建一个大型应用程序,并且觉得需要使用全局变量,那么我会建议为您的全局变量创建一个单独的模块。这应该有助于您在一个地方跟踪它们。

+1

+1 7年后仍然有用。但是,在对象变量和数据变量方面还有一些额外的细微差别吗?我遇到了一个与引发新问题的对象变量范围相关的问题。非常感谢,如果你有时间看看。 https://stackoverflow.com/q/46058096/5457466 – Egalth 2017-09-05 19:22:55

12

如果此功能位于模块/类中,则可以将它们写在函数之外,因此它有Global Scope。全局范围是指变量可以被另一个功能在同一个模块/类访问(如果你使用dim的声明语句,使用public如果希望变量可以被所有的功能在所有模块进行访问):

Dim iRaw As Integer 
Dim iColumn As Integer 

Function find_results_idle() 
    iRaw = 1 
    iColumn = 1 
End Function 

Function this_can_access_global() 
    iRaw = 2 
    iColumn = 2 
End Function 
1

在通用声明中创建一个公共整数。

然后在你的函数中,你可以每次增加它的值。 请参阅示例(将电子邮件的附件保存为CSV的功能)。

Public Numerator As Integer 

Public Sub saveAttachtoDisk(itm As Outlook.MailItem) 
Dim objAtt As Outlook.Attachment 
Dim saveFolder As String 
Dim FileName As String 

saveFolder = "c:\temp\" 

    For Each objAtt In itm.Attachments 
      FileName = objAtt.DisplayName & "_" & Numerator & "_" & Format(Now, "yyyy-mm-dd H-mm-ss") & ".CSV" 
         objAtt.SaveAsFile saveFolder & "\" & FileName 
         Numerator = Numerator + 1 

      Set objAtt = Nothing 
    Next 
End Sub 
9

这个问题真的是关于范围的问题,就像其他人说的那样。

总之,考虑这个“模块”:

Public Var1 As variant  'Var1 can be used in all 
          'modules, class modules and userforms of 
          'thisworkbook and will preserve any values 
          'assigned to it until either the workbook 
          'is closed or the project is reset. 

Dim Var2 As Variant  'Var2 and Var3 can be used anywhere on the 
Private Var3 As Variant ''current module and will preserve any values 
          ''they're assigned until either the workbook 
          ''is closed or the project is reset. 

Sub MySub()    'Var4 can only be used within the procedure MySub 
    Dim Var4 as Variant ''and will only store values until the procedure 
End Sub     ''ends. 

Sub MyOtherSub()   'You can even declare another Var4 within a 
    Dim Var4 as Variant ''different procedure without generating an 
End Sub     ''error (only possible confusion). 

你可以看看这个MSDN reference更多关于变量声明,这等Stack Overflow Question更多关于变量是如何走出去的范围。其他

两个快速件事:

  1. 使用工作簿级变量的时候,让你的代码没有得到混淆举办。首选函数(具有适当的数据类型)或传递参数ByRef
  2. 如果您希望变量在调用之间保留其值,则可以使用静态语句。
+0

你确定全局变量可以在不同的工作簿中使用吗?不适合我 – Seb 2017-03-16 15:11:23

+0

好点!我注意到我没有为我获得该信息的地方添加参考......我也没有再找到它。更好地编辑答案......:/哦,谢谢Seb。 – FCastro 2017-03-17 18:21:33