我在Userform中有复选框,并且选中了复选框,我想选择/激活与复选框对应的Excel工作表。
实施例。单击复选框A,B,C我想选择/激活选项卡A,B,C,以便我可以将信息传输到这些工作表。我知道如何传输数据,但我不确定如何根据复选框的条件选择多张纸。
If A_Checkbox.value = True Then
Cells(emptyRow, 1).value=NOD_Text.value
但问题是我有大约8个复选框,我不确定如何将数据传输到多张表格,具体取决于点击的复选框...
是否有一个函数我可以说“如果任何复选框值为true,那么将用户表单数据传输到相应的表格中?
所以我使用了响应中的代码,但我似乎无法让它工作? (我对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
答案 0 :(得分:2)
假设您的复选框对象名为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
<强> REVISED 强>
看起来您正在根据复选框选择将相同的数据从用户表单转储到每个工作表。您不需要为此选择case case语句,只需根据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控件)发送到此子例程。
布尔语句If cb
只是评估复选框是否已被选中。你可以做If cb.Value = True
,但我个人的偏好是简化它。
更新&amp;检测过强>
这是一张带有示例userform的Before图片,其中包含三个复选框和一些虚拟文本框:
现在,按下“添加”按钮后,这里是工作表“C”:
最后,我可以继续更改文本框值并反复按下添加按钮,如下所示:
答案 1 :(得分:0)
非常感谢David Zemens!
我不得不对他的代码做一些修改,因为我无法根据复选框选择粘贴多张图片。
请参阅下面的修改后的代码 - 现在我可以选择任何复选框,并将保存按钮粘贴到每张工作表的空白页中。
Private Sub
Dim cb As Control
Dim ws As Worksheet
Dim emptyRow As Long
For Each cb In UserForm3.Controls
If TypeName(cb) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
If cb Then
Set ws = Sheets(Left(cb.Name, 2))
emptyRow = (WorksheetFunction.CountA(ws.Range("A7:A5000")) + 6) + 1
With ws
.Cells(emptyRow, 1).Value = TextBox1.Value
.Cells(emptyRow, 2).Value = TextBox2.Value
.Cells(emptyRow, 3).Value = TextBox3.Value
.Cells(emptyRow, 4).Value = TextBox6.Value
.Cells(emptyRow, 5).Value = TextBox4.Value
.Cells(emptyRow, 6).Value = TextBox5.Value
End With
End If
End If
Next cb
Unload UserForm3
UserForm2.Show
End Sub