函数或子将新行和数据添加到表

我想创build一个Sub,基本上允许我使用特定名称来定位Excel表格,然后在底部插入一个新行,并将数据同时添加到该行。 然后退出子。 如果该表只有一行而没有数据,则将该数据添加到该行,然后退出该子。

我怎样才能做到这一点?

我正在用伪代码思考这样的事情:

Public Sub addDataToTable(ByVal strTableName as string, ByVal strData as string, ByVal col as integer) ActiveSheet.Table(strTableName).Select If strTableName.Rows.Count = 1 Then strTableName(row, col).Value = strData Else strTable(lastRow, col).Value = strData End if End Sub 

这可能是无效的代码,但它应该解释我至less在!

这是你想要的?

 Option Explicit Public Sub addDataToTable(ByVal strTableName As String, ByVal strData As String, ByVal col As Integer) Dim lLastRow As Long Dim iHeader As Integer With ActiveSheet.ListObjects(strTableName) 'find the last row of the list lLastRow = ActiveSheet.ListObjects(strTableName).ListRows.Count 'shift from an extra row if list has header If .Sort.Header = xlYes Then iHeader = 1 Else iHeader = 0 End If End With 'add the data a row after the end of the list ActiveSheet.Cells(lLastRow + 1 + iHeader, col).Value = strData End Sub 

它处理这两种情况,无论你是否有头。

我需要这个相同的解决scheme,但是如果你使用本地的ListObject.Add()方法,那么你就避免了与表格下面的任何数据冲突的风险。 下面的例程检查表格的最后一行,如果它是空白的,就在那里添加数据。 否则会在表的最后添加一个新行:

 Sub AddDataRow(tableName As String, values() As Variant) Dim sheet As Worksheet Dim table As ListObject Dim col As Integer Dim lastRow As Range Set sheet = ActiveWorkbook.Worksheets("Sheet1") Set table = sheet.ListObjects.Item(tableName) 'First check if the last row is empty; if not, add a row If table.ListRows.Count > 0 Then Set lastRow = table.ListRows(table.ListRows.Count).Range For col = 1 To lastRow.Columns.Count If Trim(CStr(lastRow.Cells(1, col).Value)) <> "" Then table.ListRows.Add Exit For End If Next col Else table.ListRows.Add End If 'Iterate through the last row and populate it with the entries from values() Set lastRow = table.ListRows(table.ListRows.Count).Range For col = 1 To lastRow.Columns.Count If col <= UBound(values) + 1 Then lastRow.Cells(1, col) = values(col - 1) Next col End Sub 

要调用函数,请传递表的名称和值的数组,每列一个值。 您至less可以在Excel 2013中从function区的“ Design选项卡中获取/设置表格的名称: 在这里输入图像说明

具有三列的表的示例代码:

 Dim x(2) x(0) = 1 x(1) = "apple" x(2) = 2 AddDataRow "Table1", x 

杰夫的答案稍有差异。

数组中的新数据:

 Sub AddDataRow(tableName As String, NewData As Variant) Dim sheet As Worksheet Dim table As ListObject Dim col As Integer Dim lastRow As Range Set sheet = Range(tableName).Parent Set table = sheet.ListObjects.Item(tableName) 'First check if the last row is empty; if not, add a row If table.ListRows.Count > 0 Then Set lastRow = table.ListRows(table.ListRows.Count).Range If Application.CountBlank(lastRow) < lastRow.Columns.Count Then table.ListRows.Add End If End If 'Iterate through the last row and populate it with the entries from values() Set lastRow = table.ListRows(table.ListRows.Count).Range For col = 1 To lastRow.Columns.Count If col <= UBound(NewData) + 1 Then lastRow.Cells(1, col) = NewData(col - 1) Next col End Sub 

水平范围内的新数据:

 Sub AddDataRow(tableName As String, NewData As Range) Dim sheet As Worksheet Dim table As ListObject Dim col As Integer Dim lastRow As Range Set sheet = Range(tableName).Parent Set table = sheet.ListObjects.Item(tableName) 'First check if the last table row is empty; if not, add a row If table.ListRows.Count > 0 Then Set lastRow = table.ListRows(table.ListRows.Count).Range If Application.CountBlank(lastRow) < lastRow.Columns.Count Then table.ListRows.Add End If End If 'Copy NewData to new table record Set lastRow = table.ListRows(table.ListRows.Count).Range lastRow.Value = NewData.Value End Sub 

phillfri答案的微小变化已经是Geoff答案的变化了:我增加了处理完全空白表格的能力,这些表格不包含数据码。

 Sub AddDataRow(tableName As String, NewData As Variant) Dim sheet As Worksheet Dim table As ListObject Dim col As Integer Dim lastRow As Range Set sheet = Range(tableName).Parent Set table = sheet.ListObjects.Item(tableName) 'First check if the last row is empty; if not, add a row If table.ListRows.Count > 0 Then Set lastRow = table.ListRows(table.ListRows.Count).Range If Application.CountBlank(lastRow) < lastRow.Columns.Count Then table.ListRows.Add End If End If 'Iterate through the last row and populate it with the entries from values() If table.ListRows.Count = 0 Then 'If table is totally empty, set lastRow as first entry table.ListRows.Add Position:=1 Set lastRow = table.ListRows(1).Range Else Set lastRow = table.ListRows(table.ListRows.Count).Range End If For col = 1 To lastRow.Columns.Count If col <= UBound(NewData) + 1 Then lastRow.Cells(1, col) = NewData(col - 1) Next col End Sub