【发布时间】: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”,如果它们不是其他适用的单元格应该是空白的。更新了原始帖子中的代码。