【问题标题】:Transferring Data from User Form to Worksheet将数据从用户表单传输到工作表
【发布时间】:2023-01-10 16:40:28
【问题描述】:

我有一个用户表单,用于输入员工姓名,并带有用于培训的复选框。

当用户输入员工姓名时,该值应填充到与 M 合并的 L 中。
对于选中的任何复选框,我想将“x”返回到这些培训的相应列中。

请注意,在该数据集下方的一行有另一个数据集,但标题相同,但对于 Journeymen 而不是 Foreman(这就是为什么我下面的代码引用 AZ2,它捕获上部数据集的最后一行,我计划对底部数据执行相同的操作放)。

数据集示例:

我的代码即使只针对员工姓名也不起作用。

Private Sub Submit_Click()
    Set act = ThisWorkbook.ActiveSheet
    bot_row = act.Range("AZ2")
    act.Range("L" & bot_row & ":AB" & bot_row).Insert Shift:=xlShiftDown
    act.Range("L" & bot_row & ":M" & bot_row).Value = EmpNameTextBox.Text
End Sub

编辑:工作代码

Private Sub Submit_Click()
    Dim act As Worksheet
    Set act = ThisWorkbook.ActiveSheet
    bot_row = act.Range("AZ2")
    
    act.Range("L" & bot_row & ":AB" & bot_row).Insert Shift:=xlShiftDown
    act.Range("L9:AB9").Copy
    act.Range("L" & bot_row & ":AB" & bot_row).PasteSpecial xlPasteFormats
    act.Range("L" & bot_row & ":AB" & bot_row).PasteSpecial xlPasteFormulas
    Range("P" & bot_row & ":AB" & bot_row).ClearContents
    Range("L" & bot_row) = EmpName.Value
    Range("P" & bot_row) = EmpPhone.ValueDim cBox As Control
    For Each cBox In Me.Controls
      If TypeOf cBox Is msforms.CheckBox Then
         'potential test msgbox
         'MsgBox "Box " & cBox.Caption & " has a click value = " & cBox.Value
            If cBox.Value Then
            If cBox.Caption = "Competent" Then
                Range("Q" & bot_row).Value = "x"
            ElseIf cBox.Caption = "OSHA 30hr" Then
                Range("R" & bot_row).Value = "x"
            ElseIf cBox.Caption = "OSHA 10hr" Then
                Range("S" & bot_row).Value = "x"
            ElseIf cBox.Caption = "CPR" Then
               Range("T" & bot_row).Value = "x"
            ElseIf cBox.Caption = "Hand Signal" Then
               Range("U" & bot_row).Value = "x"
            ElseIf cBox.Caption = "Rigging" Then
               Range("V" & bot_row).Value = "x"
            ElseIf cBox.Caption = "Asbestos" Then
               Range("W" & bot_row).Value = "x"
            ElseIf cBox.Caption = "Certa Torch" Then
               Range("X" & bot_row).Value = "x"
            ElseIf cBox.Caption = "Scaffold" Then
               Range("Y" & bot_row).Value = "x"
            ElseIf cBox.Caption = "Fork/Lull" Then
               Range("Z" & bot_row).Value = "x"
            ElseIf cBox.Caption = "Manlift" Then
               Range("AA" & bot_row).Value = "x"
            ElseIf cBox.Caption = "ATV" Then
               Range("AB" & bot_row).Value = "x"
            End If
         End If
           
      End If
    Next

    Unload Me
End Sub

【问题讨论】:

  • 您可能会受益于使用实际的 Microsoft Form,然后将该数据推送到将更新您的电子表格的表中。看起来您已经在当前流程上投入了很多,但 Excel 的 VBA 表单有局限性,并且比简单的 Web 表单更难开发。祝你好运。
  • 著名的。但是我正在取得进步。如果用户表单复选框被选中,我只需要帮助弄清楚如何在适用的单元格中生成“x”,如果它们不是其他适用的单元格应该是空白的。更新了原始帖子中的代码。

标签: excel vba


【解决方案1】:

这可能会奏效。我建议您在最后将其包含在您的代码中。我还建议对列使用更动态的东西。命名它们很容易,并确保您可以轻松更新而无需对 VBA 代码进行过多修改。

 Dim cBox As Control
    For Each cBox In Me.Controls
      If TypeOf cBox Is msforms.CheckBox Then
         'potential test msgbox
         'MsgBox "Box " & cBox.Caption & " has a click value = " & cBox.Value
         If cBox.Value Then
            If cBox.Caption = "CPR" Then
               Range("T" & bot_row).Value = "X"
            ElseIf cBox.Caption = "Hand Signal" Then
               Range("U" & bot_row).Value = "X"
      
            'etc.
            End If
         End If
           
      End If
    Next

【讨论】:

  • 对不起,我没有早点回应。它就像一个魅力。我一定会按照您的建议清理命名。谢谢,在编辑下的原始帖子中更新了代码。
  • 这么快的问题,我为 Chicago Resident & Apprentice 添加了一个复选框。我尝试添加以下代码来应用条件格式,但是如果您也能帮我解决这个问题,它就无法正常工作了?这是我一直在尝试的当前代码:ElseIf cBox.Caption = "Chicago Resident" Then Range("L" & bot_row & ":AB" & bot_row).FormatConditions(1).Interior.Color = vbYellow ElseIf cBox.Caption = "Apprentice" Then Range("L" & bot_row).FormatConditions(1).Font.Color = vbRed
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2014-03-06
  • 2017-04-21
相关资源
最近更新 更多