我在用户窗体中有复选框,并且根据选择了哪些复选框我想选择/激活与复选框相对应的 Excel 工作表。
前任。单击复选框 A、B、C 我想选择/激活选项卡 A、B、C,以便我可以将信息传输到这些工作表。我知道如何传输数据,但我不确定如何在给定复选框条件的情况下选择多张工作表。
If A_Checkbox.value = True Then
Cells(emptyRow, 1).value=NOD_Text.value
但问题是我有大约 8 个复选框,我不确定如何根据单击的复选框将数据传输到多个工作表中......
是否有一个功能可以让我说“如果任何复选框的值为真,那么将用户表单数据传输到相应的工作表中?
所以我使用了响应中的代码,但我似乎无法让它工作? (我对 vba 不是很熟悉..对不起...)
Private Sub Add_Button_Click ()
Dim ctrl As Control
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "Checkbox" Then
Transfervalues ctrl, emptyRow
End If
Next
End Sub
Function Transfervalues(cb As MSForms.CheckBox, emptyRow As Long)
Dim ws As Worksheet
If cb Then
Select Case cb.Name
Case "A"
Sheets("A").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("A").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("A").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("A").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("A").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("A").Cells(emptyRow, 6).Value = CPN_Text.Value
Case "B"
Sheets("B").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("B").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("B").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("B").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("B").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("B").Cells(emptyRow, 6).Value = CPN_Text.Value
Case "C"
Sheets("C").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("C").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("C").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("C").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("C").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("C").Cells(emptyRow, 6).Value = CPN_Text.Value
Case "D"
Sheets("D").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("D").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("D").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("D").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("D").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("D").Cells(emptyRow, 6).Value = CPN_Text.Value
Case "E"
Sheets("E").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("E").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("E").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("E").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("E").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("E").Cells(emptyRow, 6).Value = CPN_Text.Value
Case "F"
Sheets("F").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("F").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("F").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("F").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("F").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("F").Cells(emptyRow, 6).Value = CPN_Text.Value
Case "G"
Sheets("G").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("G").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("G").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("G").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("G").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("G").Cells(emptyRow, 6).Value = CPN_Text.Value
Case "H"
Sheets("H").Cells(emptyRow, 1).Value = NOD_Text.Value
Sheets("H").Cells(emptyRow, 2).Value = TOD_Text.Value
Sheets("H").Cells(emptyRow, 3).Value = Program_Text.Value
Sheets("H").Cells(emptyRow, 4).Value = email_Text.Value
Sheets("H").Cells(emptyRow, 5).Value = OPN_Text.Value
Sheets("H").Cells(emptyRow, 6).Value = CPN_Text.Value
End Select
End If
End Function
最佳答案
假设您的复选框对象名为 A_Checkbox
, B_Checkbox
等,对应于名称与 "A"
完全相同的工作表, "B"
等,然后类似:
Private Sub Add_Button_Click()
Dim ctrl As Control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
End Sub
修订
看起来您正在根据复选框选择将相同的数据从用户表单转储到每个工作表。为此,您不需要 case select 语句,只需定义
worksheet
基于 CheckBox.Name
的变量.请注意,我将其从 Function
更改为到 Sub
虽然那不应该真的很重要。我也将其更改为 emptyRow
的值每次都会计算,因为这会根据您正在处理的工作表而改变。Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow as Long
If cb Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 1))
emptyRow = WorksheetFunction.CountA(ws.Range("A:A")) + 1
With ws
.Cells(emptyRow, 1).Value = NOD_Text.Value
.Cells(emptyRow, 2).Value = TOD_Text.Value
.Cells(emptyRow, 3).Value = Program_Text.Value
.Cells(emptyRow, 4).Value = email_Text.Value
.Cells(emptyRow, 5).Value = OPN_Text.Value
.Cells(emptyRow, 6).Value = CPN_Text.Value
End With
End If
End Sub
根据 OP 的评论进行编辑以澄清
TypeName
是一个内置方法,它返回一个标识对象类型的字符串。在这种情况下,我们会遍历用户表单上的所有控件,因此您需要一些逻辑来确保该函数仅在 CheckBox
上运行。控制。cb
是 TransferValues
的局部变量子程序。在调用子例程中(在我的示例中为 CommandButton1_Click
),我们发送对象 ctrl
(一个 CheckBox 控件)到这个子例程。bool 语句
If cb
仅评估复选框是否已被选中。你可以做If cb.Value = True
但我个人的偏好是简化它。更新和测试
这是一个包含三个复选框和一些虚拟文本框的示例用户表单的之前图片:
现在这是我按下“添加”按钮后的工作表“C”:
最后,我可以继续更改文本框的值并一遍又一遍地按下添加按钮,如下所示:
关于vba - 如何选择/激活多个工作表以将用户表单数据传输到单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286105/