VBA公共变量

时间:2015-08-04 15:42:15

标签: excel vba excel-vba public

我在公共场合遇到麻烦,因为我的代码是整数部分。 我正在使用i保留当前行的值,以便我可以使用此范围 我的节目。在我的for循环中,它递增i,因此它将逐步通过一列并搜索v 然而,当我尝试在另一组代码中使用“i”时,“i”不再具有值。 我不确定全局/公共变量在VBA中是如何工作的,或者是什么导致了这个错误。

问题发生在Sub“是”和sub“no” 在代码

Cells(i,lcol).value=" ok " 

Cells(i,lcol).value = " updated "

第一组代码如下,获取“i”

的值
Public V As Integer
Public i As Integer

Private Sub Enter_Click()
Dim EmptyRow As Long

'Audit will only search Master Sheet
Worksheets("Master").Activate

'Find empty row value so we can use that for limit of search
With Sheets("Master")
     EmptyRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
End With

'i needs to be set to minimum limit
'Begin loop of search
For i = 12 To EmptyRow + 1

    If Cells(i, 1).Value = V Then  'AssetNum.Value Then

        'Go to compare userform to display

        Compare.AssetDisplay.Value = AssetNum.Value
        Compare.LocationDisply.Value = Cells(i - 1, 2).Value
        Compare.Show

     End If
  Next i

    'If i gets to emptyrow num then go to non found asset userform
    Unload Me
    NonFoundAsset.Show

 End Sub

Private Sub UserForm_Initialize()

'Read in value from asset num to be comapre in loop
AssetNum.Value = V

End Sub

我尝试使用公共变量调用“i”的第二组代码,它没有值

Private Sub No_Click()

Dim ws As Worksheet
Dim lcol As Long

'Make Master Sheet Active
 Worksheets("Master").Activate

Set ws = ThisWorkbook.Sheets("Master")

'Finds next empty column

  With ws
        lcol = .Cells(11, .Columns.Count).End(xlToLeft).Column - 1
  End With

'If the displayed location is not the same as the actual location "No" will be
'selected and the Change User Form will be displayed
'The value under the current audit column will be displayed as updated
 Cells(i, lcol).Value = " Updated "
 Unload Me
 AuditChange.Show

End Sub

Private Sub Yes_Click()

 Dim ws As Worksheet
 Dim lcol As Long

'Make Master Sheet Active
 Worksheets("Master").Activate

Set ws = ThisWorkbook.Sheets("Master")

'Finds next empty column

  With ws
        lcol = .Cells(11, .Columns.Count).End(xlToLeft).Column - 1
    End With

    'If the location displayed is correct "Yes" will be selected and
    'The value "ok" will be displayed in the current audit column

    Cells(i, lcol).Value = " Ok "
    Unload Me
    'Returns to Assetlookup to look for a new asset
    Assetlookup.Show
End Sub

我感谢任何帮助,我是VBA的新手并且不明白为什么这不起作用。

2 个答案:

答案 0 :(得分:4)

我相信UserForm中的公共变量只有在UserForm运行(加载)时才可用。要拥有一个真正的全局变量,请在普通模块中声明它。

可能该变量不可用,VB无法在其范围内找到它。如果工具,选项,需要变量声明变为关闭,VB将在当前范围内创建一个具有该名称的新变量。因此,它看起来好像已经“失去”了它的价值。

提示:不要调用像i, j, n等类似的全局变量。这些变量通常用作循环和计数器的局部变量。使用命名约定,使变量是全局变量。我总是在{global}之前加上g这样的变量,例如:

Public giLoopCounter As Integer;

答案 1 :(得分:3)

这取决于您声明它的位置。你必须参考那个位置。因此,如果我在UserForm1中并且您尝试从其他表单使用它,请将其引用为。

Cells(UserForm1.i,lcol).value=" ok " 

如果你把

Option explicit

在您尝试从中调用它的表单的顶部会告诉您,我自己并未在您的代码范围内定义。

编辑:有关OP的其他评论。被问及我是否可以在点击活动中公开。

据我所知,您不能在事件中拥有公共/全局变量。

您必须使用变量local

'Public variables are declared outside (above) all subs and functions
'This will be accessible by all subs functions and events in in the forms or sheets module or wherever it is
Public i As Integer

'This will be accessible by all subs functions and events in in the CURRENT sheet or form. It is private but to the current item
Private i As Integer

Private Sub CommandButton1_Click()
    Dim j As count

    'Do whatever it is to get that value.
    j = 5

    'You can access i to use it in you click event code
    msgbox i * j

    'Or you can set it in the event
    i = j

End Sub