-1
我有一個問題,在Excel 2010中下面一個窗體是代碼:Excel 2010個的用戶窗體VBA
Private Sub Label1_Click()
End Sub
Private Sub Userform_Initialize()
'EmptyRoleTextbox
RoleTextBox.Value = ""
'EmptyNameTextBox1
NameTextBox1.Value = ""
'EmptyDirTextbox1
DirTextBox1.Value = ""
'EmptyRemarksTextBox1
RemarksTextBox1.Value = ""
'EmptyAWSComboBox
AWSComboBox.Clear
'FillAWSComboBox
With AWSComboBox
.AddItem "MA"
.AddItem "Combat"
.AddItem "Cbt Sp"
.AddItem "CSS"
.AddItem "Comd Sp"
.AddItem "AM"
.AddItem "Medical"
.AddItem "Military Police"
.AddItem "Baker Street"
.AddItem "Putney"
.AddItem "Sloane Square"
.AddItem "Kings Cross"
.AddItem "Whitechapel"
.AddItem "Holland Park"
End With
End Sub
Private Sub Add_Click()
Dim emptyRow As Long
Dim LastRow As Long
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Cells(LastRow + 1, "A").Value = ActiveSheet.Cells(LastRow, "A").Value + 1
'Transfer information
If AWSComboBox.Value = "MA" Then
Sheet1.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Combat" Then
Sheet2.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Cbt Sp" Then
Sheet3.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "CSS" Then
Sheet4.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Comd Sp" Then
Sheet5.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "AM" Then
Sheet6.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Medical" Then
Sheet7.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Military Police" Then
Sheet8.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Baker Street" Then
Sheet9.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Putney" Then
Sheet10.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Sloane Square" Then
Sheet11.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Kings Cross" Then
Sheet12.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Whitechapel" Then
Sheet13.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
ElseIf AWSComboBox.Value = "Holland Park" Then
Sheet14.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A"))
LastRow = WorksheetFunction.CountA(Range("A:A"))
Cells(emptyRow, 2).Value = RoleTextBox.Value
Cells(emptyRow, 3).Value = NameTextBox1.Value
Cells(emptyRow, 4).Value = DirTextBox1.Value
Cells(emptyRow, 5).Value = RemarksTextBox1.Value
ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
End If
Unload Me
End Sub
Private Sub Cancel_Click()
enter code here
Unload Me
End Sub
基本上在組合框中每個項目是一個單獨的工作表。當我在框中輸入信息時,我想讓它進入空行中正確的工作表。但是,所有工作表中的列A都是項目的序列號,因此有關LastRow的部分會自動指出該列中的其他編號。
現在,我的問題是,如果我選擇不同的工作表(即「MA」),但我填寫詳細信息以轉到「Cbt」工作表,它將覆蓋該工作表中的最後一行。但是,如果我已經選擇了正確的紙張,那麼它沒有問題。
我有一種感覺,它是與ActiveSheet。不知道如何解決它。
始終使用範圍和細胞具有所有者表對象,即Sheet1.Range( 「A:A」),Sheet1.Cells(emptyRow,2)。 – smozgur
或與ActiveSheet,因爲您已經激活每個部分ActiveSheet.Range(「A:A」),ActiveSheet.Cells(emptyRow,2)中的工作表。方面的評論:你的代碼可以縮短很多。 – smozgur
聲明並直接處理要自動化的對象。 [避免使用Select](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)。 – L42