سؤال

The user can enter up to 10 members at once.

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"

I have trouble trying to input the userform values to the worksheet.

'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

This is the input to the worksheet. enter image description here

This is what the UserForm looks like

enter image description here

هل كانت مفيدة؟

المحلول

I've built a UserForm similar to yours and named the controls on it accordingly

enter image description here

now, double clicked the command button and used this code

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

which produced

enter image description here


Me.Hide hides the form instead of unloading it. Therefore the data on the form should still be accessible from the Module's code.

In your mode you load the form like this

UserForm1.Show

and then when you're done fetching data from it (again, that's in the Module no under the button's event) you unload it

Unload UserForm1

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top