【发布时间】:2014-01-22 14:48:44
【问题描述】:
我在用户窗体中有复选框,并且根据选择了哪些复选框,我想选择/激活与复选框相对应的 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
【问题讨论】:
-
试试
Select Case Left(cb.Name, 1)。另外我会根据你所做的对我的做一些修改。