excel - 在 VBA (Excel) 中访问全局变量

标签 excel vba variables global

我从未在 VBA 中使用过全局变量,但我知道全局变量是在函数/子声明之外实例化的?

我在模块顶部声明了一个全局(公共(public))变量,然后由同一模块内的子例程为其赋予值 0。

Option Explicit
Public NumNodes As Integer

Sub Inst_Glob_Vars()
NumNodes = 0
End Sub

每当工作簿打开时都会调用此子例程(在“ThisWorkbook”对象中调用 sub),它还将实例化全局变量并设置 0 值。

Option Explicit

Private Sub Workbook_Open()
Call Inst_Glob_Vars
End Sub

我在 Excel 工作表中有一个按钮,单击该按钮时,将增加此全局变量。此按钮的定义位于 Sheet1 对象中。

Private Sub CommandButton2_Click()
'NumNodes = NumNodes + 1
Debug.Print "NumNodes = " & NumNodes 'Debug
End Sub

我需要在使用变量的每个模块/对象中声明全局/公共(public)变量吗?每次单击按钮时,变量都不会递增,而是在调试时给出 Null/Blank 值。我肯定没有正确声明我的全局变量,但不确定我在哪里犯了错误。

更新:这是更新的命令按钮子。如果我注释掉第二个子调用(Node_Button_Duplication),一切正常。很可能是那个子程序导致了问题......

Private Sub CommandButton2_Click()
Call Channel_Selection_Duplication
Call Node_Button_Duplication
NumNodes = NumNodes + 1
Debug.Print "NumNodes = " & NumNodes 'Debug
End Sub

Channel_Selection_Duplication 和 Node_Button_Duplication 都在同一个单独的模块中定义:

Option Explicit

Public Sub Channel_Selection_Duplication()
'
' Description: Macro which duplicates the 'Channel Usage Selection' columns at a specific cell reference

    Range("Q8:S8").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    Range("Q8:S8").Select
    ActiveCell.FormulaR1C1 = "Channel Usage Selection"
    Range("Q8:S52").Select
    Range("Q52").Activate
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("Q8:S8").Select
    Selection.Interior.ColorIndex = 36

'NumNodes = NumNodes + 1
'Debug.Print NumNodes
End Sub

Public Sub Node_Button_Duplication()

    ActiveSheet.Shapes("CommandButton1").Select
    Selection.Copy
    Range("Q5").Select
    ActiveSheet.Paste
    Selection.ShapeRange.IncrementTop -14.25
End Sub

最佳答案

将其粘贴到模块中

Option Explicit

Public myVar As Long

将其粘贴到sheet1中的命令按钮单击事件中

Option Explicit

Private Sub CommandButton1_Click()
    myVar = myVar + 1
    MsgBox myVar
End Sub

现在尝试一下。

此外,您不需要将 Workbook_Open 中的值设置为 0 event :) 当您打开工作簿时,它默认采用值 0。

跟进

I have a feeling copying and pasting a control element in the spreadsheet somehow resets the variable. I'm currently trying to find a solution... – user1373525 6 mins ago

Yes :) Adding the button recompiles the VBA code and hence the global variables get reset. Use a Temp Sheet to hold the variables. You could also use registry to store that information :) – Siddharth Rout just now

仅当您单击该按钮两次时才会观察到此行为,而当您一次性执行该按钮时则不会。例如

Private Sub CommandButton2_Click()
    NumNodes = NumNodes + 1
    MsgBox NumNodes, vbInformation, "1"

    Node_Button_Duplication

    NumNodes = NumNodes + 1
    MsgBox NumNodes, vbInformation, "2"

    Node_Button_Duplication

    NumNodes = NumNodes + 1
    MsgBox NumNodes, vbInformation, "3"
End Sub

在这种情况下,它总是会增加该值。但是,下次单击该按钮时,您会注意到该变量已重置。

关于excel - 在 VBA (Excel) 中访问全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604459/

相关文章:

vba - 重新排序并重复某些文本列

VBA编写函数及返回值

vba - Excel 范围已复制并转置粘贴到记事本。

excel - VBA if then 函数总是返回 0

vba - 如果工作表不存在,则创建它 (VBA Excel)

.net - vb.net 中的变量/属性更改事件

ruby-on-rails - 访问用户密码: Variable Scope in Rails

javascript - var 与 function,哪个更适合常量和方法

php - 使用 Maatwebsite Laravel-Excel 加载超链接

vba - 根据用户输入应用多个过滤器 (VBA)