我找到了一个函数,可以使用VBA将记录附加到Excel中的表中。我已经实现了它,但我不熟悉Variant
数据类型。我做了一些研究,并尝试使用它,因为我在VB中使用数组,但我不断得到一个奇怪的错误,它期待=
符号。
以下是非屏幕截图形式的代码:
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
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
Sub btnNewGateway_Click()
pName = Application.InputBox("Enter New Participant Name", "New Participant")
Worksheets("TemplateGateway").Copy After:=Worksheets("TemplateGateway")
ActiveSheet.Name = pName + " Gateway"
Dim Values()
v(0) = pName
v(1) = "Gateway"
v(2) = Today()
AddDataRow ("tblOverview",????)
End Sub
答案 0 :(得分:3)
你需要使用
调用AddDataRow(tbloverview,v)