How to declare Global Variables in Excel VBA to be visible across the Workbook

前端 未结 2 858
悲&欢浪女
悲&欢浪女 2020-12-09 03:58

I have a question about global scope and have abstracted the problem into a simple example:

In an Excel Workbook: In Sheet1 I have two(2) buttons.

2条回答
  •  感情败类
    2020-12-09 04:56

    Your question is: are these not modules capable of declaring variables at global scope?

    Answer: YES, they are "capable"

    The only point is that references to global variables in ThisWorkbook or a Sheet module have to be fully qualified (i.e., referred to as ThisWorkbook.Global1, e.g.) References to global variables in a standard module have to be fully qualified only in case of ambiguity (e.g., if there is more than one standard module defining a variable with name Global1, and you mean to use it in a third module).

    For instance, place in Sheet1 code

    Public glob_sh1 As String
    
    Sub test_sh1()
        Debug.Print (glob_mod)
        Debug.Print (ThisWorkbook.glob_this)
        Debug.Print (Sheet1.glob_sh1)
    End Sub
    

    place in ThisWorkbook code

    Public glob_this As String
    
    Sub test_this()
        Debug.Print (glob_mod)
        Debug.Print (ThisWorkbook.glob_this)
        Debug.Print (Sheet1.glob_sh1)
    End Sub
    

    and in a Standard Module code

    Public glob_mod As String
    
    Sub test_mod()
        glob_mod = "glob_mod"
        ThisWorkbook.glob_this = "glob_this"
        Sheet1.glob_sh1 = "glob_sh1"
        Debug.Print (glob_mod)
        Debug.Print (ThisWorkbook.glob_this)
        Debug.Print (Sheet1.glob_sh1)
    End Sub
    

    All three subs work fine.

    PS1: This answer is based essentially on info from here. It is much worth reading (from the great Chip Pearson).

    PS2: Your line Debug.Print ("Hello") will give you the compile error Invalid outside procedure.

    PS3: You could (partly) check your code with Debug -> Compile VBAProject in the VB editor. All compile errors will pop.

    PS4: Check also Put Excel-VBA code in module or sheet?.

    PS5: You might be not able to declare a global variable in, say, Sheet1, and use it in code from other workbook (reading http://msdn.microsoft.com/en-us/library/office/gg264241%28v=office.15%29.aspx#sectionSection0; I did not test this point, so this issue is yet to be confirmed as such). But you do not mean to do that in your example, anyway.

    PS6: There are several cases that lead to ambiguity in case of not fully qualifying global variables. You may tinker a little to find them. They are compile errors.

提交回复
热议问题