循环浏览用户表单上的不同控件并读取/写入值VBA

时间:2018-10-07 09:57:11

标签: excel vba excel-vba userform

我想以某种方式从用户表单上的不同控件中获取值,然后将其写在工作表上,如果关闭了用户表单,如果在组合框中选择了名称,则将其重新打开,然后加载所有数据在表格中准备好更改值。我有13行可供用户在用户表单上使用。

Order id

user form

Sheet data

在我的代码中,将数据写入工作表将写入我想要的所有选定项目,但由于所有循环和ifs,它花费的时间太长。有没有更好的方法来实现我想要的?

    Private Sub FillingInForm()

    Dim i As Long
    Dim WS As Worksheet

    Dim ctl As MSForms.Control
    Dim lbl As MSForms.Label
    Dim cmb As MSForms.ComboBox
    Dim txtbox As MSForms.TextBox
    Dim optbtn As MSForms.OptionButton

    Set WS = ActiveSheet

With WS

    For i = 1 To ItemsListFrame.Controls.Count
        For Each ctl In ItemsListFrame.Controls
            If TypeName(ctl) = "Label" Then
                If ctl.Tag = "GroupItem" & i Then
                    Set lbl = ctl
                    If Controls("Item" & i).Value <> vbNullString Then
                        .Range("A" & i + 6).Offset(0, 0).Value = Me.OrderNo.Value
                        .Range("A" & i + 6).Offset(0, 1).Value = Me.NextCollectionDate.Text
                        .Range("A" & i + 6).Offset(0, 1).Value = Format(.Range("A" & i + 6).Offset(0, 1).Value, "dd/mm/yyyy")
                        .Range("A" & i + 6).Offset(0, 8).Value = Me.DateReturnBy.Value
                        .Range("A" & i + 6).Offset(0, 8).Value = Format(.Range("A" & i + 6).Offset(0, 8).Value, "dd/mm/yyyy")
                        Controls("OrderLbl" & i).Enabled = True
                    End If
                End If

            ElseIf TypeName(ctl) = "ComboBox" Then
                If ctl.Tag = "GroupItem" & i Then
                    Set cmb = ctl
                    If Controls("Item" & i).Value <> vbNullString Then
                        Controls("Item" & i).Enabled = True
                    End If
                    If Controls("Item" & i).Value <> vbNullString Then
                        .Range("A" & i + 6).Offset(0, 2).Value = Controls("Item" & i).Text
                    End If
                End If

            ElseIf TypeName(ctl) = "TextBox" Then
                If ctl.Tag = "GroupItem" & i Then
                    Set txtbox = ctl
                    If Controls("Item" & i).Value <> vbNullString Then
                        .Range("A" & i + 6).Offset(0, 3).Value = Controls("Qty" & i).Value
                        .Range("A" & i + 6).Offset(0, 4).Value = Controls("UnitPrice" & i).Value
                        .Range("A" & i + 6).Offset(0, 5).Value = Controls("SubTotal" & i).Value
                        .Range("A" & i + 6).Offset(0, 7).Value = Controls("Comments" & i).Value
                        Controls("Qty" & i).Enabled = True
                        Controls("UnitPrice" & i).Enabled = True
                        Controls("SubTotal" & i).Enabled = True
                        Controls("Comments" & i).Enabled = True
                    End If
                End If
            ElseIf TypeName(ctl) = "OptionButton" Then
                If ctl.Tag = "GroupItem" & i Or ctl.Tag = "InOut" & i Then
                    Set optbtn = ctl
                    If Controls("Item" & i).Value <> vbNullString Then
                        .Range("A" & i + 6).Offset(0, 6).Value = Controls("OptionOut" & i).Value
                        Controls("OptionIn" & i).Enabled = True
                        Controls("OptionOut" & i).Enabled = True
                    End If
                End If
            End If
        Next ctl
      Next i
End With

End Sub

0 个答案:

没有答案