在vba的特定行中添加数据

当我点击命令button后,我想让我的Excel做:

  • input我在文本框中键入的内容/在特定列中的combobox中select,而不删除先前input的内容

但在这个时候,它不能像我预期的那样工作,或者从文本框和comboboxinput任何input。

我写的脚本是:

Private Sub If TextBox1.Value = "" Or TextBox2.Value = "" Or TextBox3.Value = "" Then If MsgBox ("There might one or more empty cells, do you want to continue to proceed?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub End If End If Dim invsheet As Worksheet Dim pacsheet As Worksheet Set invsheet = ThisWorkbook.Sheets("INV") Set pacsheet = ThisWorkbook.Sheets("PAC") invsheet.Range("A1").Value = TextBox6.Text invsheet.Range("I5").Value = TextBox7.Text invsheet.Range("A21").Value = TextBox5.Text invsheet.Range("A25").Value = ComboBox1.Value inv_nr = invsheet.Cells(Row.Count, 1).End(xlUp).Row +1 invsheet.Cells(inv_nr, 5).Value = Me.TextBox1 invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2 pac_nr = pacsheet.Cells(Row.Count, 1).End(xlUp).Row +1 pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2 pacsheet.Cells(pac_nr, 5).Value = Me.TextBox3 pacsheet.Cells(pac_nr, 5).Value = Me.TextBox4 

问题:

 inv_nr = invsheet.Cells(Row.Count, 1).End(xlUp).Row +1 invsheet.Cells(inv_nr, 5).Value = Me.TextBox1 invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2 pac_nr = pacsheet.Cells(Row.Count, 1).End(xlUp).Row +1 pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2 pacsheet.Cells(pac_nr, 7).Value = Me.TextBox3 'mistyped it. supposed to be 7 pacsheet.Cells(pac_nr, 9).Value = Me.TextBox4 'mistyped it. supposed to be 9 

该代码块不起作用,并在工作表上创build任何输出。

我会很感激你的帮助。

谢谢!

你没有在A列中放置任何东西(除了invsheet A1,A21和A25),所以根据列A中最后使用的单元格来设置inv_nrpac_nrvariables并不是一个好主意。

试着把它放在你正在填充数据的列之一上,例如第5列:

 'Always qualify "Rows" (and don't mistype it as "Row") inv_nr = invsheet.Cells(invsheet.Rows.Count, 5).End(xlUp).Row + 1 invsheet.Cells(inv_nr, 5).Value = Me.TextBox1 invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2 'Always qualify "Rows" (and don't mistype it as "Row") pac_nr = pacsheet.Cells(pacsheet.Rows.Count, 5).End(xlUp).Row + 1 pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2 'Note: This is pointless because the next line overwrites it pacsheet.Cells(pac_nr, 5).Value = Me.TextBox3 'Note: This is pointless because the next line overwrites it pacsheet.Cells(pac_nr, 5).Value = Me.TextBox4