1
我發現了一個函數,用於使用VBA將記錄附加到Excel中的表中。我已經實現了它,但我不熟悉Variant
數據類型。我做了一些研究,並試圖使用它,因爲我會在VB中的數組,但我不斷得到一個奇怪的錯誤,它期望=
符號。將Variant數組傳遞給sub,「Expected:=」
這裏是在非截圖形式的代碼:
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
首先你要求的答案。刪除'()'。除非你使用'Call'方法或有一個你想要'='的變量,否則不需要。 –
其次,當你嘗試運行它時,你會得到一個錯誤,你需要'dim v(0 to 2)'。我不會使用Values,因爲它接近保護詞,但這是首選。 –