从用户窗体到模块的公共数组是空的,但在用户窗体内部它获取值
Posted
技术标签:
【中文标题】从用户窗体到模块的公共数组是空的,但在用户窗体内部它获取值【英文标题】:Public array from an userform to module is empty, but inside the userform it get the values 【发布时间】:2017-05-25 10:26:10 【问题描述】:我遇到了一个奇怪的问题。我有 2 个Userform
,起初Userform2
在Userform1
里面,但我无法让它运行。在那之后,我在我的模块调用 Userform2
中创建了一个 Sub 在其他一次 Userform1
完成之后。
问题是Userform2
是一个多选Listbox
,我将每个选择保存在一个数组中,当 Userform2 卸载时,该数组似乎为空。
我已经检查了几个小时的许多错误,例如:我没有明确的选项,但是代码很短,我已经在我的代码中查找了数组,它只出现在我想要的确切部分。
作为变体的公共数组都不是问题,因为它在用户窗体之外和原始模块中。
我来自智利,所以有些变量用西班牙语命名,我的英语可能不太好,我会尽力解释我自己。
代码如下:
Global lenarrv
Global Arrver
Global BoxPMData
Global anno
Global PMDATAarray
Global LenPMDArray
Public varfamilia As Variant
Public pampm As Variant
Sub AbrirMaestro(ByVal anno As Variant)
Application.ScreenUpdating = False
UserForm1.Show
UserForm2.Show
Dim Arrver() As Variant
Dim lenarrv As Long
contentosrcontento = 1
lenarrv = UBound(pampm)
For tt = 0 To lenarrv - 1
ReDim Arrver(contentosrcontento)
Arrver(contentosrcontento) = pampm(tt)
contentosrcontento = contentosrcontento + 1
Next tt
lenarrv = UBound(Arrver)
Inputbox_PMDATA anno
End Sub
用户表单来了:
Private Sub UserForm_Activate()
Me.StartUpPosition = 1
Me.Left = Application.Left + Application.Width - Me.Width - 400
End Sub
Private Sub UserForm_Initialize()
Dim listaitems As Variant
Workbooks("MAESTROS.xlsx").Activate
numerofilas = Workbooks("MAESTROS.xlsx").Sheets("VERTICALES").Cells(2, 1).End(xlDown).Row
listaitems = Workbooks("MAESTROS.xlsx").Sheets("VERTICALES").Range("A2:A" & numerofilas)
listaitems = Application.WorksheetFunction.Transpose(listaitems)
For h = 1 To numerofilas - 1
ListBox1.AddItem listaitems(h)
Next h
Workbooks("MAESTROS.xlsx").Close False
End Sub
Public Sub CommandButton1_Click()
Dim i As Integer
Dim count As Integer
count = 1
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ReDim pampm(count)
pampm(i) = ListBox1.List(i)
count = count + 1
End If
Next i
Unload UserForm2
End Sub
这里是UserForm1
:
Private Sub UserForm_Activate()
Me.StartUpPosition = 1
Me.Left = Application.Left + Application.Width - Me.Width - 400
End Sub
Private Sub UserForm_Initialize()
Dim ListItems As Variant
MsgBox ("Elegir el archivo MAESTRO")
Master = Application.GetOpenFilename(MultiSelect:=False)
isBool = VarType(Master) = vbBoolean
If isBool Then If Not Master Then End
Application.ScreenUpdating = False
Set abrirlibro = Workbooks.Open(Master)
numrows = abrirlibro.Worksheets("MASTERFAMILIA").Cells(2, 2).End(xlDown).Row
ListItems = abrirlibro.Worksheets("MASTERFAMILIA").Range("B2:B" & numrows)
ListItems = Application.WorksheetFunction.Transpose(ListItems)
For i = 1 To numrows - 1
ComboBox1.AddItem ListItems(i) ' populate the listbox
Next i
'abrirlibro.Close False
'Set abrirlibro = Nothing
End Sub
Public Sub CommandButton1_Click()
varfamilia = ComboBox1.value
Unload UserForm1
End Sub
我无法通过完整的代码,因为有超过 2k 行..
欢迎任何帮助。
您好!
【问题讨论】:
【参考方案1】:你必须改变
Public pampm As Variant
到
Public pampm() As Variant
和
ReDim pampm(count)
到
ReDim Preserve pampm(count)
【讨论】:
我已经尝试过了,但遗憾的是,即使我在pamp(count)
之后添加 as Variant
,我也会收到错误“类型不匹配(错误 13)”
你成功了!谢谢你,天哪,我太天真了以上是关于从用户窗体到模块的公共数组是空的,但在用户窗体内部它获取值的主要内容,如果未能解决你的问题,请参考以下文章
为啥从标准模块(而不是用户窗体)调用 VBA 代码时运行得更快?