添加另一行数据

时间:2015-01-21 17:29:28

标签: excel vba excel-vba

对于正确的方法,我看起来很高,但是我遇到了很多麻烦。

我正在尝试在表的末尾添加另一行数据,但它所做的只是添加另一行但不添加实际数据。

Sub CommandButton1_Click()
Dim lastRow As Long
Dim iHeader As Integer
Dim strData As String
strname = InputBox(prompt:="Enter data")
    Set myTable = ActiveSheet.ListObjects("Table1")
    ActiveSheet.ListObjects("Table1").ListRows.Add
         With ActiveSheet.ListObjects("Table1")
            lastRow = ActiveSheet.ListObjects("Table1").ListRows.Count
        End With
    ActiveSheet.Cells(lastRow, "M").Value = strData    
End Sub 

它只会向表中添加一行,但无论我尝试什么,它都不会添加strData值。用于测试目的的列是“M”

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

看起来像是一个印刷错误:(这就是为什么你应该总是使用Option Explicit

Option Explicit
Sub CommandButton1_Click()
    Dim lastRow As Long
    Dim iHeader As Integer
    Dim strData As String
    strData = InputBox(prompt:="Enter data") 'Fixed variable here
    Set myTable = ActiveSheet.ListObjects("Table1")
    With myTable
        .ListRows.Add
        lastRow = .ListRows.Count
    End With
    ActiveSheet.Cells(lastRow, "M").Value = strData    
End Sub