我需要制作一个VBA程序,根据用户通过rollbox输入的内容,将序列号放在特定工作表的列表中。
这就是我现在所拥有的:
Private Sub ComboBox1_Change()
Dim emptyRow As Long
Dim MyRange As Range
Set MyRange = Range("M:M")
emptyRow = Application.WorksheetFunction.CountA(MyRange) + 1
If ComboBox1.Value = "BMW" Then Sheets("BMW").Cells(emptyRow, 1).Value = serial_textbox.Text
If ComboBox1.Value = "AUDI" Then Sheets("AUDI").Cells(emptyRow, 1).Value = serial_textbox.Text
If ComboBox1.Value = "VW" Then Sheets("VW").Cells(emptyRow, 1).Value = serial_textbox.Text
If ComboBox1.Value = "Porsche" Then Sheets("Porsche").Cells(emptyRow, 1).Value = serial_textbox.Text
If ComboBox1.Value = "Lamborghini" Then Sheets("Lamborghini").Cells(emptyRow, 1).Value = serial_textbox.Text
End Sub
不幸的是,它只输入A1中的值(在正确的工作表中)并将其相互粘贴。
答案 0 :(得分:0)
试试这个。您可以缩短代码,因为在每种情况下您都做同样的事情,而且工作表名称与组合框列表一致。
Private Sub ComboBox1_Change()
Dim emptyRow As Long
emptyRow = WorksheetFunction.Max(10, Sheets(ComboBox1.Value).Range("M" & Rows.Count).End(xlUp).Row + 1)
Sheets(ComboBox1.Value).Cells(emptyRow, "M").Value = serial_textbox.Text
End Sub
答案 1 :(得分:0)
这个怎么样:
Private Sub ComboBox1_Change()
Dim emptyRow As Long
Dim MyRange As Range
Dim wsSheet As Excel.Worksheet
Set wsSheet = ThisWorkbook.Worksheets(ComboBox1.Value)
Set MyRange = wsSheet.Range("M:M")
emptyRow = Application.WorksheetFunction.CountA(MyRange) + 1
wsSheet.Cells(emptyRow, 1).Value = serial_textbox.Text
End Sub