0

用户一次最多可以输入 10 个成员。A 列将是“团队名称” B 列将是“成员数量” C 列将是“成员名称” 它适用于 If Else 但对我来说做 10 次很乏味。我不知道如何将其从 If Else 更改为 For Loop

'Using If Else
    Dim RowCount As Long
    RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
    If txtNoMember.Value = 1 Then
        With Worksheets("Sheet1").Range("A1")
            .Offset(RowCount, 0).Value = txtTeamName.Text
            .Offset(RowCount, 1).Value = txtNoMember.Text
            .Offset(RowCount, 2).Value = txtMember01.Text
        End With
    ElseIf txtNoMember.Value = 2 Then
        With Worksheets("Sheet1").Range("A1")
            .Offset(RowCount, 0).Value = txtTeamName.Text
            .Offset(RowCount, 1).Value = txtNoMember.Text
            .Offset(RowCount, 2).Value = txtMember01.Text
            .Offset(RowCount + 1, 0).Value = txtTeamName.Text
            .Offset(RowCount + 1, 1).Value = txtNoMember.Text
            .Offset(RowCount + 1, 2).Value = txtMember02.Text
        End With
    End If

'Using For Loop
    Dim counter As Integer
    Dim times As Integer
    Dim RowCount As Long
    RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
       For counter = 1 To txtNoMember.Value
            times = txtNoMember.Value - 1
            With Worksheets("Sheet1").Range("A1")
                .Offset(RowCount + times, 0).Value = txtTeamName.Text
                .Offset(RowCount + times, 1).Value = txtNoMember.Text
                .Offset(RowCount + times, 2).Value = txtMember01.Text
            End With
4

1 回答 1

0

正如我评论的那样,通过<FormName>.Controls(<ControlName>). 用一个简单的形式演示:

用户窗体1

代码:

Private Sub CommandButton1_Click()
    Dim i As Long, m As Long

    m = CLng(txtNoMember.Value)
    For i = 1 To m
        Debug.Print UserForm1.Controls("TextBox" & Format(i, "00")).Value
    Next
End Sub

将 10 放入 txtNoMember 并单击命令按钮时,立即窗口给出:

立即窗口

现在您需要锻炼第 i 个成员与循环索引之间的数学运算,以便该行存储值。

粗略看看您将使用的代码是(从 01 到 10 的文本框):

.Offset(RowCount + times, 0).Value = <FormName>.Controls("txtMember" & Format(times + 1, "00")).Text

于 2013-11-15T05:02:52.833 回答