试图查看这个,但我仍然是VBA的新手,仍然很困惑。我无法弄清楚如何从一个子变量中获取变量并在另一个子变量中使用它。如何从模块中的子变量中使用变量,并在用户窗体的另一个子功能中使用Excel VBA
我想从GetListBox1Items
获取变量ListBox1Items
并在cbSave_Click
中使用它。我一直收到Set oNewRow = Selection.ListObject.ListRows.Add(1)
的错误。我试过Dim ListBox1Items As String
和Public ListBox1Items As String
但这没有帮助。
是否子模块的位置? GetListBox1Items
在模块中。 cbSave_Click
位于UserForm中。
我抬头看着使用类型,但它变得混乱。
Private Sub cbSave_Click()
Dim oNewRow As ListRow
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Creatures").Range("MonsterList")
Set oNewRow = Selection.ListObject.ListRows.Add(1)
With ws
Call GetListBox1
oNewRow.Range.Cells(1, 24).Value = Me.StatBox1.Value
oNewRow.Range.Cells(1, 35).Value = ListBox1Items
End With
End Sub
和GetListBox1是
Sub GetListBox1()
Dim SelectedItems As String
Dim ListBox1Items As String
With MonsterMaker
For i = 0 To .ListBox1.ListCount - 1
If .ListBox1.Selected(i) = True Then
SelectedItems = SelectedItems & .ListBox1.List(i) & ", "
End If
Next i
ListBox1Items = Left(SelectedItems, Len(SelectedItems) - 2)
End With
End Sub
您需要将其设为全局变量。一些指针https://stackoverflow.com/questions/2722146/how-do-i-declare-a-global-variable-in-vba – QHarr
你可以让GetListBox1函数返回ListBox1Items的值 – mooseman
尝试设置全局变量在用户窗体和模块中,但仍然出现'Set oNewRow = Selection.ListObject.ListRows.Add(1)'错误。 – bigbucky