Tried looking this up but I'm still new to VBA and still pretty confused. I can't figure out how to get the variable from one sub and use it in another sub.
I want to get the variable ListBox1Items
from GetListBox1Items
and use it in cbSave_Click
. I keep getting an error on Set oNewRow = Selection.ListObject.ListRows.Add(1)
. I tried Dim ListBox1Items As String
and Public ListBox1Items As String
but that doesn't help.
Does the module location of the sub matter? GetListBox1Items
is in a Module. cbSave_Click
is in a UserForm.
I looked up using Types but it got confusing.
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
and GetListBox1 is
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