【问题标题】:Loop Through Userform & Paste to Offset Cells循环遍历用户表单并粘贴以偏移单元格
【发布时间】:2016-05-04 14:22:03
【问题描述】:

又是我!

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

这是我最近的尝试:

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

如果有人能帮我弄清楚如何解决这个问题,那就太好了!

【问题讨论】:

  • 为什么叫 ufField.Value 而它应该是 ufControl.value? (如果 TypeOf ufControl IS....)
  • 啊,这是我给它的旧命名约定,但 ufControl 更有意义,所以我改变了它。谢谢你发现那个。但是错误可能在于我试图用来实现我想要的方法?

标签: vba for-loop controls each userform


【解决方案1】:

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

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。你至少可以尝试一次:)

【讨论】:

  • 这似乎会产生:运行时错误'438':对象不支持此属性或方法...最近在用户表单中得到了很多。这个错误就行了:idCell.Offset(0, Counter).Value = ufControl.Result
  • 将“idCell.Offset(0,Counter).Value”更改为“idCell.CurrentRegion.Offset(0,Counter).Value”一秒钟,看看是否仍然抛出错误。跨度>
  • 我选择了您的回复作为答案,因为我只需要进行很小的更改即可使其正常工作。我只是将 .result 和 .value 更改为 .text ,这似乎可以解决问题。非常感谢您的帮助:)
【解决方案2】:

我设法通过使用 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

非常感谢您的帮助:)

【讨论】:

  • 哈哈哈好吧,我不认为这会出错,但很高兴我能帮助你弄清楚如何让它工作;)
猜你喜欢
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多