根据在用户窗体上的组合框中所做的选择,我如何将所做的选择发送到所需的工作表。例如,我们在一个工作簿中有 12 个工作表。每个工作表名称都是一年中一个月的名称。组合框选择是一年中的所有月份。一月至十二月。如果我在组合框中选择一月,我希望用户窗体上的所有其他条目都转到一月工作表。如果我选择二月,我希望其他文本框中的所有条目都转到二月工作表,依此类推。我该如何做到这一点?任何帮助将不胜感激!!!
我重新编辑了代码。现在唯一的问题是 cellVal4 = Me.tbDate.Text 错误在于 .Text 突出显示。此外,当我提交表单时,它不会为每个条目添加新行,它会更改第一个条目所在的同一行。
Option Explicit
将 WrkSheet 调暗为工作表
Private Sub btnSubmit_Click() Application.EnableEvents = False
Dim ssheet As Workbook 将 cellVal1 作为字符串,cellVal2 作为字符串,cellVal3 作为字符串,cellVal4 作为字符串,cellVal5 作为字符串,cellVal6 作为字符串,cellVal7 作为字符串,cellVal8 作为字符串,cellVal9 作为字符串,cellVal10 作为字符串,cellVal11 作为字符串,cellVal12作为字符串将 cellVal13 作为字符串,cellVal14 作为字符串,cellVal15 作为字符串,cellVal16 作为字符串,cellVal17 作为字符串,cellVal18 作为字符串,cellVal19 作为字符串,cellVal20 作为字符串,cellVal21 作为字符串,cellVal22 作为字符串将 cellVal23 作为字符串,cellVal24 作为字符串, cellVal25 作为字符串, cellVal26 作为字符串, cellVal27 作为字符串, cellVal28 作为字符串, cellVal29 作为字符串, cellVal30 作为字符串, cellVal31 作为字符串, cellVal32 作为字符串, cellVal33 作为字符串, cellVal34 作为字符串
Dim shtCmb As String
Dim RwLast As Long
shtCmb = Me.cmbListItem1.Value
If shtCmb = "" Then
MsgBox "Please choose a month.", vbOKOnly
Me.cmbListItem1.SetFocus
End If
cellVal1 = Me.cmbListItem1.Text
cellVal2 = Me.cmbListItem2.Text
cellVal3 = Me.cmbListItem3.Text
cellVal4 = Me.TextBox31.Text
cellVal5 = Me.TextBox1.Text
cellVal6 = Me.TextBox2.Text
cellVal7 = Me.TextBox3.Text
cellVal8 = Me.TextBox4.Text
cellVal9 = Me.TextBox5.Text
cellVal10 = Me.TextBox6.Text
cellVal11 = Me.TextBox7.Text
cellVal12 = Me.TextBox8.Text
cellVal13 = Me.TextBox9.Text
cellVal14 = Me.TextBox10.Text
cellVal15 = Me.TextBox11.Text
cellVal16 = Me.TextBox12.Text
cellVal17 = Me.TextBox13.Text
cellVal18 = Me.TextBox14.Text
cellVal19 = Me.TextBox15.Text
cellVal20 = Me.TextBox16.Text
cellVal21 = Me.TextBox17.Text
cellVal22 = Me.TextBox18.Text
cellVal23 = Me.TextBox19.Text
cellVal24 = Me.TextBox20.Text
cellVal25 = Me.TextBox21.Text
cellVal26 = Me.TextBox22.Text
cellVal27 = Me.TextBox23.Text
cellVal28 = Me.TextBox24.Text
cellVal29 = Me.TextBox25.Text
cellVal30 = Me.TextBox26.Text
cellVal31 = Me.TextBox27.Text
cellVal32 = Me.TextBox28.Text
cellVal33 = Me.TextBox29.Text
cellVal34 = Me.TextBox30.Text
RwLast = Worksheets(shtCmb).Range("AI" & Worksheets(shtCmb).Rows.Count).End(xlUp).Row
Worksheets(shtCmb).Range("AI" & RwLast + 1).Value = cellVal1
Worksheets(shtCmb).Range("AJ" & RwLast + 1).Value = cellVal2
Worksheets(shtCmb).Range("A" & RwLast + 1).Value = cellVal3
Worksheets(shtCmb).Range("AH" & RwLast + 1).Value = cellVal4
Worksheets(shtCmb).Range("B" & RwLast + 1).Value = cellVal5
Worksheets(shtCmb).Range("C" & RwLast + 1).Value = cellVal6
Worksheets(shtCmb).Range("D" & RwLast + 1).Value = cellVal7
Worksheets(shtCmb).Range("E" & RwLast + 1).Value = cellVal8
Worksheets(shtCmb).Range("F" & RwLast + 1).Value = cellVal9
Worksheets(shtCmb).Range("G" & RwLast + 1).Value = cellVal10
Worksheets(shtCmb).Range("H" & RwLast + 1).Value = cellVal11
Worksheets(shtCmb).Range("I" & RwLast + 1).Value = cellVal12
Worksheets(shtCmb).Range("J" & RwLast + 1).Value = cellVal13
Worksheets(shtCmb).Range("K" & RwLast + 1).Value = cellVal14
Worksheets(shtCmb).Range("L" & RwLast + 1).Value = cellVal15
Worksheets(shtCmb).Range("M" & RwLast + 1).Value = cellVal16
Worksheets(shtCmb).Range("N" & RwLast + 1).Value = cellVal17
Worksheets(shtCmb).Range("O" & RwLast + 1).Value = cellVal18
Worksheets(shtCmb).Range("P" & RwLast + 1).Value = cellVal19
Worksheets(shtCmb).Range("Q" & RwLast + 1).Value = cellVal20
Worksheets(shtCmb).Range("R" & RwLast + 1).Value = cellVal21
Worksheets(shtCmb).Range("S" & RwLast + 1).Value = cellVal22
Worksheets(shtCmb).Range("T" & RwLast + 1).Value = cellVal23
Worksheets(shtCmb).Range("U" & RwLast + 1).Value = cellVal24
Worksheets(shtCmb).Range("V" & RwLast + 1).Value = cellVal25
Worksheets(shtCmb).Range("W" & RwLast + 1).Value = cellVal26
Worksheets(shtCmb).Range("X" & RwLast + 1).Value = cellVal27
Worksheets(shtCmb).Range("Y" & RwLast + 1).Value = cellVal28
Worksheets(shtCmb).Range("Z" & RwLast + 1).Value = cellVal29
Worksheets(shtCmb).Range("AA" & RwLast + 1).Value = cellVal30
Worksheets(shtCmb).Range("AB" & RwLast + 1).Value = cellVal31
Worksheets(shtCmb).Range("AC" & RwLast + 1).Value = cellVal32
Worksheets(shtCmb).Range("AD" & RwLast + 1).Value = cellVal33
Worksheets(shtCmb).Range("AF" & RwLast + 1).Value = cellVal34
Application.EnableEvents = True
End Sub
Private Sub cmbListItem1_Change()
End Sub
Private Sub optionCancel_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim SH As Worksheet
Dim Entry As Variant
' MonthName(Month(Now)) - Will return the name of the current Month
For Each SH In ThisWorkbook.Worksheets
If SH.Name = MonthName(month(Now)) Then
Set WrkSheet = SH
Exit For
End If
Next
'fill the combo box
With Me.cmbListItem1
For Each Entry In [List1]
.AddItem Entry
Next Entry
.Value = MonthName(month(Now))
End With
'fill the combo box
With Me.cmbListItem2
For Each Entry In [List2]
.AddItem Entry
Next Entry
End With
'fill the combo box
With Me.cmbListItem3
For Each Entry In [List3]
.AddItem Entry
Next Entry
End With
End Sub