2010-04-27 32 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