Reputation:
I found a function to append a record to a table in Excel using VBA. I have implemented it but I'm not familiar with the Variant
data type. I did some research and am attempting to use it as I would an array in VB but I keep getting a weird error that it's expecting an =
sign.
Here is the code in non-screenshot form:
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
Upvotes: 1
Views: 616