![](/img/trans.png)
[英]How do I send data from my userform to a seprate spreadsheet in different cells
[英]How do i select/activate multiple sheets to transfer Userform data into cells?
我在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
假設您的復選框對象名為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語句,只需根據CheckBox.Name
定義worksheet
變量。 請注意,我將其從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
但我個人的偏好是簡化它。
更新和測試
這是一張帶有示例userform的Before圖片,其中包含三個復選框和一些虛擬文本框:
我按下“添加”按鈕后,這里是工作表“C”:
最后,我可以繼續更改文本框值並反復按下添加按鈕,如下所示:
非常感謝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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.