如何select/激活多个工作表以将Userform数据传送到单元格中?

我有UserForm中的checkbox,并select哪个checkbox被选中,我想select/激活对应于checkbox的Excel工作表。

防爆。 checkboxA,B,C被点击我想select/激活标签A,B,C,所以我可以将信息转移到这些表单。 我知道如何trasnfer数据,但我不确定如何select多个工作表给予checkbox的条件。

If A_Checkbox.value = True Then Cells(emptyRow, 1).value=NOD_Text.value 

但问题是我有大约8checkbox,我不确定如何传输数据到多个工作表取决于点击checkbox…

有一个函数,我可以说:“如果任何checkbox值是真的,然后将用户表单数据转换成相应的表?


所以我用了响应代码,但我似乎无法得到它的工作? (我不是很熟悉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_CheckboxB_Checkbox等的checkbox对象与名称完全类似"A""B"等的工作B_Checkbox对应,则类似:

 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 

修订

看起来您正在根据checkboxselect将相同的数据从用户表单转储到每个工作表。 你不需要case select语句,只需要定义一个基于CheckBox.Nameworksheetvariables。 请注意,我将其从一个Function改为了一个Sub尽pipe这并不重要。 我也改变这个,所以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是一个内置的方法,它返回一个标识对象types的string。 在这种情况下,我们迭代用户表单上的所有控件,因此您需要一些逻辑来确保该函数仅在CheckBox控件上运行。

cbTransferValues子例程的局部variables。 在调用子程序(在我的例子中, CommandButton1_Click ),我们发送对象ctrl (一个CheckBox控件)到这个子例程。

布尔语句If cb只是评估checkbox是否已被选中。 你可以做If cb.Value = True但我个人的偏好是简化它。

更新和testing

这里是一个包含三个checkbox和几个虚拟文本框的示例用户窗体之前的图片:

在这里输入图像说明

按下“添加”button后,这里是工作表“C”:

在这里输入图像说明

最后,我可以继续改变文本框的值,并反复按下添加button,如下所示:

在这里输入图像说明

非常感谢David Zemens!

我不得不稍微修改他的代码,因为我不能根据checkboxselect粘贴多个工作表。

看到下面的修改代码 – 现在我可以select我的任何checkbox,并保存button粘贴到每个工作表的空行。

 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 

在这里input图像说明