Userform包含工作表的Textbox和Checkbox输入

时间:2013-12-05 07:36:01

标签: excel vba userform

用户一次最多可以输入10个成员。

Column A will be "Team Number"
Column B will be "Number of Member" 
Column C will be "Member Name" 
Column D will be "Month Available" 
Column E will be "Number of Family Members Coming" 
Column F will be "Family Members"

我无法尝试将userform值输入到工作表中。

'inputValue
    Dim RowCount As Long
    Dim rStart As Long
    Dim rFirstEnd As Long
    Dim rLastEnd As Long
    RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
        rMemberEnd = CLng(txtNoMember.Value)
        rMonthEnd = CLng(txtNoMember.Value)
        rFamilyMemberEnd = CLng(txtNoFamilyMemberValue)
        For rStart = 1 To rMemberEnd
            With Worksheets(“Sheet1").Range("A1")
                .Offset(RowCount + rStart, 0).Value = txtTeamNo.Text
                .Offset(RowCount + rStart , 1).Value = txtNoMember.Text
                .Offset(RowCount + rStart , 2).Value = Controls("txtMemberName”  & Format(rStart, "00")).Value
            For rStart = 1 To rMonthEnd
                With Worksheets(“Sheet1").Range("A1")
                    If Controls ("chkMonth”  & Format(rStart, "00")).Value = True Then
                        .Offset(RowCount + rStart , 3).Value = CLng(Right$(Controls("chkMonth”  & Format(rStart, "00")).Name, 2))
                        .Offset(RowCount + rStart , 4).Value = txtNoFamilyMember.Text
                    For rStart = 1 To rFamilyMemberEnd
                            With Worksheets(“Sheet1").Range("A1")
                                .Offset(RowCount + rStart , 5).Value = Controls("txtFamilyMember" & Format(rStart, "00")).Text
                            End With
                    End If
                End With
            End With
        Next

这是工作表的输入。 enter image description here

这就是UserForm的样子

enter image description here

1 个答案:

答案 0 :(得分:0)

我已经构建了一个类似于你的UserForm并相应地命名了它的控件

enter image description here

现在,双击命令按钮并使用此代码

Private Sub CommandButton1_Click()

    Dim ws As Worksheet
    Set ws = Sheets("Sheet1")

    Dim members As Long
    members = CLng(txtNoMember.Value)

    Dim family As Long
    family = CLng(txtNoFamilyMember.Value)

    Dim months As Long
    Dim i As Long
    For i = 1 To 12
        If Controls("chkMonth" & Format(i, "00")).Value = True Then
            months = months + 1
        End If
    Next i

    Dim total As Long
    total = members * months * family

    Dim j As Long, k As Long, m As Long, n As Long

    For i = 1 To members

        For j = 1 To total / members
            ws.Range("A" & ws.Range("A" & Rows.Count).End(xlUp).Row + 1) = CLng(txtTeamNo)
            ws.Range("B" & ws.Range("B" & Rows.Count).End(xlUp).Row + 1) = members
            ws.Range("C" & ws.Range("C" & Rows.Count).End(xlUp).Row + 1) = Controls("txtMemberName" & Format(i, "00")).Value
            ws.Range("E" & ws.Range("E" & Rows.Count).End(xlUp).Row + 1) = family
        Next j

        For j = 1 To months
            For m = 1 To family
                If Len(Controls("txtFamilyMember" & Format(m, "00")).Text) <> vbNullString Then
                    ws.Range("F" & ws.Range("F" & Rows.Count).End(xlUp).Row + 1) = Controls("txtFamilyMember" & Format(m, "00")).Text
                End If
            Next m
        Next j

        For j = 1 To 12
            If Controls("chkMonth" & Format(j, "00")).Value = True Then
                ws.Range("D" & ws.Range("D" & Rows.Count).End(xlUp).Row + 1) = CLng(Right$(Controls("chkMonth" & Format(j, "00")).Name, 2))
                ws.Range("D" & ws.Range("D" & Rows.Count).End(xlUp).Row).Resize(family, 1).Formula = ws.Range("D" & ws.Range("D" & Rows.Count).End(xlUp).Row).Formula
            End If
        Next j

    Next i

    Me.Hide
End Sub

产生了

enter image description here


Me.Hide隐藏表单而不是卸载它。因此,仍然可以从模块的代码访问表单上的数据。

在您的模式中,您可以像这样加载表单

UserForm1.Show

然后当你完成从中获取数据时(再次,在按钮事件下的模块中),你将其卸载

Unload UserForm1