How do I declare a global variable in VBA?

前端 未结 8 1072

I wrote the following code:

Function find_results_idle()

    Public iRaw As Integer
    Public iColumn As Integer
    iRaw = 1
    iColumn = 1
相关标签:
8条回答
  • 2020-11-22 09:04

    If this function is in a module/class, you could just write them outside of the function, so it has Global Scope. Global Scope means the variable can be accessed by another function in the same module/class (if you use dim as declaration statement, use public if you want the variables can be accessed by all function in all modules) :

    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
    
    0 讨论(0)
  • 2020-11-22 09:07

    Create a public integer in the General Declaration.

    Then in your function you can increase its value each time. See example (function to save attachements of an email as 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
    
    0 讨论(0)
  • 2020-11-22 09:08

    Also you can use -

    Private Const SrlNumber As Integer = 910
    
    Private Sub Workbook_Open()
        If SrlNumber > 900 Then
            MsgBox "This serial number is valid"
        Else
            MsgBox "This serial number is not valid"
        End If
    End Sub
    

    Its tested on office 2010

    0 讨论(0)
  • 2020-11-22 09:09

    You need to declare the variables outside the function:

    Public iRaw As Integer
    Public iColumn As Integer
    
    Function find_results_idle()
        iRaw = 1
        iColumn = 1
    
    0 讨论(0)
  • 2020-11-22 09:10

    To use global variables, Insert New Module from VBA Project UI and declare variables using Global

    Global iRaw As Integer
    Global iColumn As Integer
    
    0 讨论(0)
  • 2020-11-22 09:18

    This is a question about scope.

    If you only want the variables to last the lifetime of the function, use Dim (short for Dimension) inside the function or sub to declare the variables:

    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
    

    A global variable (as SLaks pointed out) is declared outside of the function using the Public keyword. This variable will be available during the life of your running application. In the case of Excel, this means the variables will be available as long as that particular Excel workbook is open.

    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.
    

    You can also have variables that are only accessible within a particular module (or class) by declaring them with the Private keyword.

    If you're building a big application and feel a need to use global variables, I would recommend creating a separate module just for your global variables. This should help you keep track of them in one place.

    0 讨论(0)
提交回复
热议问题