VBA公共variables

作为我的代码的一部分,我遇到了麻烦。 我正在使用我来保持我当前行的值,所以我可以在我的程序中使用这个范围。 在我的循环,它增加我,所以它会逐步通过一列和searchv然而,当我尝试使用“我”在另一组代码“我”不再有一个值。 我不知道如何全局/公共variables在VBA中工作,或者是什么原因导致这个错误。

该问题发生int小组“是”,并在代码分“不”

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

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

第一套代码如下,这是我的价值为“我”

 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 

第二组代码即时通讯设法使用公共variables调用“我”,它没有价值

 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,不明白为什么这是行不通的。

这取决于你在哪里申报。 你必须参考那个位置。 所以,如果我在UserForm1中,并且您正在尝试从另一个表单使用它,请将其作为参考。

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

如果你放

 Option explicit 

在你试图从它调用它的forms的顶部会告诉你,我本身没有在你的代码范围内定义。

编辑:来自OP的额外评论。 问我是否可以在点击事件公开。

据我所知,你不能在事件中有公共/全局variables。

你将不得不使用一个可变的本地

 '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 

我相信UserForm中的公有variables只有在用户窗体正在运行(加载)时才可用。 要有一个真正的全局variables,在一个正常的模块中声明它。

可能variables不可用,VB无法在其范围内find它。 如果工具,选项,要求variables声明closures ,VB将在当前范围内创build一个具有该名称的新variables。 因此,它看起来好像已经“失去了”它的价值。

提示:不要像i, j, n等调用全局variables。它们通常用作循环和计数器的局部variables。 使用一个命名约定来清除variables是全局的。 我总是在g这个variablesg “global”,例如:

 Public giLoopCounter As Integer;