在VBA中访问全局variables(Excel)

我从来没有在VBA中使用过全局variables,但是我知道全局variables是在函数/子声明之外实例化的吗?

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

Option Explicit Public NumNodes As Integer Sub Inst_Glob_Vars() NumNodes = 0 End Sub 

只要打开工作簿(在“ThisWorkbook”对象中调用子),就会调用此子例程,该子例程也将实例化全局variables并设置0值。

 Option Explicit Private Sub Workbook_Open() Call Inst_Glob_Vars End Sub 

我在Excel表单中有一个button,点击后会增加这个全局variables。 该button的定义在Sheet1对象中。

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

我是否需要在variables的每个模块/对象中声明全局/公共variables? 每次点击button,variables都不会递增,而是在debugging时给出一个空/空值。 我肯定没有正确地声明我的全局variables,但不知道我在哪里犯错误。

更新:这里是更新的命令button子。 如果我注释掉第二个子调用(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中的命令button单击事件中

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

现在尝试一下。

您也不需要在Workbook_Open事件中将该值设置为0 :)打开工作簿时默认值为0。

跟进

我有一种感觉,复制和粘贴电子表格中的控件元素,以某种方式重置variables。 我目前正在试图find一个解决scheme… – user1373525 6分钟前

是的:)添加button重新编译的VBA代码,因此全局variables得到重置。 使用临时表来保存variables。 你也可以使用registry来存储这些信息:) – 刚刚Siddharth Rout

只有当您单击button两次,而不是一次执行时才会出现此行为。 例如

 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 

在这种情况下,它总会增加价值。 但是,下次单击button时,您将注意到该variables已被重置。