excel 2010 userforms vba

时间:2016-05-16 09:19:37

标签: excel vba excel-vba

我在Excel 2010中遇到Userform问题。以下是代码:

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有关。不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

而不是使用ActiveSheet(因为正如您所经历的那样,它可能会根据您的需要而改变),创建对所需工作表的引用并对其进行处理。示例如下: -

Private Sub MyWorkSheet()
Dim WkSht As Excel.Worksheet

set WkSht = ThisWorkbook.WorkSheets(Me.AWSComboBox.Value)
    LastRow = WkSht.Range("A" & Rows.Count).End(xlUp).Row
Set WkSht = Nothing

End Sub