Loop Through Userform&粘贴到偏移单元格

时间:2016-01-27 14:56:05

标签: vba for-loop controls each userform

又是我了!

我试图通过循环遍历每个控件并通过带计数器的偏移将数据粘贴到单元格中,将数据输入到带有用户表单的数据库中。我在实际上将数据输入到单元格的行上得到错误,并且无法通过循环弄清楚如何执行此操作。逐字逐行是很容易的,但我不想写那么多行代码。

这是我最近的尝试:

Option Explicit

Sub cbSubmit_Click()
' Set worksheet
Dim dbFood As Worksheet
Set dbFood = Sheets("dbFood")

'Set last row and column
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Row

Dim lCol As Long
lCol = Cells(1, Columns.Count).End(xlLeft).Row

'Define idCell as Range type
Dim idCell As Range

' If no records exit, add first record
If Cells(lRow, 1).Value = "ID" Then
    Set idCell = dbFood.Range("A2")
    idCell.Value = 1

' Add Data
Dim ufControl As Control
Dim Counter As Long
Counter = 1

For Each ufControl In Me.Controls

If TypeOf ufControl Is MSForms.ComboBox Or MSForms.TextBox Then
idCell.Offset(0, Counter).Value = ufField.Value
Counter = Counter + 1

End If

Next ufControl

MsgBox "Added to database!"

' Else add next record
ElseIf Cells(lRow, 1).Value >= 0.1 Then
    Dim lastID As Long
    lastID = Cells(lRow, 1).Value

    Set idCell = dbFood.Cells(lRow + 1, 1)
    idCell.Value = lastID + 1

' Add Data


' If none of the above display ERROR and exit sub
Else: MsgBox ("ERROR - Cannot Create Record")
Exit Sub

End If

End Sub

如果有人能帮助我弄清楚如何解决这个问题那么太棒了!

2 个答案:

答案 0 :(得分:1)

我设法通过使用Kathara向我建议的方法来解决这个问题但编辑它以避免438错误。以下是我为使其发挥作用所做的小幅调整:

For Each ufControl In Me.Controls

        If TypeOf ufControl Is MSForms.TextBox Then
            idCell.Offset(0, Counter).Value = ufControl.Text
            Counter = Counter + 1
        ElseIf TypeOf ufControl Is MSForms.ComboBox Then
            idCell.Offset(0, Counter).Value = ufControl.Text
            Counter = Counter + 1
        End If

    Next ufControl

非常感谢你的帮助:)。

答案 1 :(得分:0)

我看到了一些我在下面修改过的东西。我可以请你测试那段代码吗?

Option Explicit

Sub cbSubmit_Click()

    Dim dbFood As Worksheet
    Set dbFood = ActiveWorkbook.Sheets("dbFood")

    Dim lRow As Long
    lRow = dbFood.Cells(dbFood.Rows.Count, 1).End(xlUp).Row

    Dim lCol As Long
    lCol = dbFood.Cells(1, dbFood.Columns.Count).End(xlLeft).Row

    Dim idCell As Range


    If dbFood.Cells(lRow, 1).Value = "ID" Then
        Set idCell = dbFood.Range("A2")
        idCell.Value = 1

        Dim ufControl As Control
        Dim Counter As Long
        Counter = 1

        For Each ufControl In Me.Controls

            If TypeOf ufControl Is MSForms.TextBox Then
                idCell.Offset(0, Counter).Value = ufControl.Result
                Counter = Counter + 1
            ElseIF TypeOf ufControl Is MSForms.ComboBox
                idCell.Offset(0, Counter).Value = ufControl.SeletedItem.Value
            End If

       Next ufControl

       MsgBox "Added to database!"

    ElseIf dbFood.Cells(lRow, 1).Value >= 0.1 Then
        Dim lastID As Long
        lastID = dbFood.Cells(lRow, 1).Value

        Set idCell = dbFood.Cells(lRow + 1, 1)
        idCell.Value = lastID + 1

    Else
        MsgBox ("ERROR - Cannot Create Record")
        Exit Sub
    End If

End Sub

正如你所看到的,我已经划分了ufcontrol的类型,因为我不确定使用组合框你可以直接说.Value所以你必须添加.SelectedItem。你至少可以尝试一次:)