创建并添加动态数组

创建并添加动态数组

我正在尝试为自己协助运营的体育俱乐部创建一个登录用户表单,但在添加到动态数组时遇到了问题。

    Private Sub UserForm_Initialize()

Dim SignedInNames() As String
Dim NumberSignedIn As Integer
    NumberSignedIn = 1

ReDim SignedInNames(NumberSignedIn + 1)


Dim Names() As Variant
Names = Range("OFFSET('People Info'!$A$1,1,0,COUNTA('People Info'!$A:$A)-1,1)")

PersonNameComboBox.List = Names

SignedInListBox.List = SignedInNames
End Sub

Private Sub SignInCommandButton_Click()

SignedInNames = Add(PersonNameComboBox.Value) 'The code crashers here
NumberSignedIn = NumberSignedIn + 1

End Sub

我已经改了,所以现在是

Private Sub UserForm_Initialize()

Dim SignedInNames() As String
Dim NumberSignedIn As Integer
    NumberSignedIn = 1

ReDim SignedInNames(0 To NumberSignedIn)


Dim Names() As Variant
Names = Range("OFFSET('People Info'!$A$1,1,0,COUNTA('People Info'!$A:$A)-1,1)")

PersonNameComboBox.List = Names

SignedInListBox.List = SignedInNames
End Sub

Private Sub SignInCommandButton_Click()

ReDim Preserve SignedInNames(0 To NumberSignedIn)
SignedInNames(NumberSignedIn) = PersonNameComboBox.Value
NumberSignedIn = NumberSignedIn + 1
'ReDim SignedInNames(o To NumberSignedIn)
SignedInListBox.List = SignedInNames
'SignedInNames = Add(PersonNameComboBox.Value)
'SignedInNames(NumberSignedIn) = PersonNameComboBox.Value
'NumberSignedIn = NumberSignedIn + 1

End Sub

我现在收到一个错误,当它到达行时

ReDim Preserve SignedInNames(0 To NumberSignedIn)

显示错误“变量未定义”

答案1

我通过改变设置数组的方式解决了最初的问题。然后我通过将我使用的变量设为全局变量来解决了第二个问题。

Dim SignedInNames() As String
Dim NumberSignedIn As Integer
Dim Names() As Variant

Private Sub UserForm_Initialize()


NumberSignedIn = 1

ReDim SignedInNames(0 To NumberSignedIn)


Dim Names() As Variant
Names = Range("OFFSET('People Info'!$A$1,1,0,COUNTA('People Info'!$A:$A)-1,1)")

PersonNameComboBox.List = Names

SignedInListBox.List = SignedInNames
End Sub

Private Sub SignInCommandButton_Click()



ReDim Preserve SignedInNames(0 To NumberSignedIn)
SignedInNames(NumberSignedIn) = PersonNameComboBox.Value
NumberSignedIn = NumberSignedIn + 1
SignedInListBox.List = SignedInNames


End Sub

相关内容