vba - 如何选择/激活多个工作表以将用户表单数据传输到单元格?

标签 vba excel userform

我在用户窗体中有复选框,并且根据选择了哪些复选框我想选择/激活与复选框相对应的 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 上运行。控制。
cbTransferValues 的局部变量子程序。在调用子例程中(在我的示例中为 CommandButton1_Click),我们发送对象 ctrl (一个 CheckBox 控件)到这个子例程。

bool 语句 If cb仅评估复选框是否已被选中。你可以做If cb.Value = True但我个人的偏好是简化它。

更新和测试

这是一个包含三个复选框和一些虚拟文本框的示例用户表单的之前图片:

enter image description here

现在这是我按下“添加”按钮后的工作表“C”:

enter image description here

最后,我可以继续更改文本框的值并一遍又一遍地按下添加按钮,如下所示:

enter image description here

关于vba - 如何选择/激活多个工作表以将用户表单数据传输到单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286105/

相关文章:

ms-access - 如何在Access 2010 VBA中封装IIF和ISNULL调用?

excel - VBA:从另一张表中获取引用值作为过滤条件

vba - 列格式转换为数字,但显示为文本

excel - 如何拆分段落并导出到 Excel

excel - 反向循环 - 将单元格倒数到 1

Excel VBA - 直到最后一行的日期

excel - 在 Excel VBA 中使用偏移量时出错

excel - 单击用户窗体上的按钮(调用按钮单击事件)

excel - 在用户表单中旋转图片

excel - 关闭工作簿时出现内存不足错误 - Excel VBA