我正在尝试在excel中填充一个列表,我可以使用vba和一维数组连接到power查询。
因此,用户将列表放入文本框中,其中每个项目用分号分隔,然后将其带入数组。到目前为止,我有:
Dim arrSIOCodes As Variant
arrSIOCodes = Split(tbSIOCodes, ";")
ThisWorkbook.Sheets("CAEATFA_SIO").Activate
Call ChangeTableToArray(tbl:=ThisWorkbook.Sheets("CAEATFA_SIO").ListObjects("tblSIOCodes"), ar:=arrSIOCodes)
我从另一个堆栈溢出帖子中得到了这个子,但是我需要根据我的问题自定义它并且我遇到了麻烦
Sub ChangeTableToArray(tbl As ListObject, ar)
Dim newRows As Long: newRows = 1 + UBound(ar, 1) - LBound(ar, 1)
If Not tbl.DataBodyRange Is Nothing Then tbl.DataBodyRange.EntireRow.Delete
If newRows > 1 Then tbl.HeaderRowRange.Resize(newRows - 1).Offset(2).EntireRow.Insert
tbl.HeaderRowRange.Resize(newRows, 1 + UBound(ar, 1) - LBound(ar, 1)).Offset (1).value = ar
End Sub
然而,这就是我需要做的事情:
答案 0 :(得分:1)
您要调整目标中列的数量,但只有一列(如果您Transpose
数组)要存储。所以做一些像:
tbl.HeaderRowRange.Resize(newRows, 1).Offset (1).value = Application.Transpose(ar)
答案 1 :(得分:0)
即将进入一个迟到的会议,所以我很快把它扔到一起,这是未经测试的。但这只是另一种看待它的方式:
Sub ArrayToTable(ByVal ws As Worksheet, ParamArray YourArr() As Variant)
Dim TmpArr As Variant, i As Long, LineNum As Long
TmpArr = YourArr(0)
LineNum = 1 'whatever method you use to find linenumbers
For i = LBound(TmpArr) To UBound(TmpArr)
ws.Cells(LineNum, 1).Value = TmpArr(i)
LineNum = LineNum + 1
Next
End Sub