我正在使用Access並需要從Excel導入數據。 但是表中有太多的列,我只需要其中的一些。現在我要做的是找出我需要的每個單元格,並將它們合併在一起,然後插入到Access中。有沒有一種簡單的方法將數據導入到VBA的Excel中進行訪問?
Set file = GetObject(path) 'get excel file
intArrNeedCols = Array(3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 26) 'the columns I need
For row = 2 To file.Sheets(1).UsedRange.Rows.Count 'loop and collect values of each cell in the target column,the first row is title
strSQL = "INSERT INTO orderlist VALUES("
For Each col In intArrNeedCols
strSQL = strSQL & "'" & file.sheets(1).cells(row, col) & "',"
Next
…… 'other statements,like excute sql statement
Next
正如你所看到的,太複雜,難以維護,我想知道是否有一個簡單的方法來做到這一點,大加讚賞,如果你可以在這裏發佈的代碼,因爲我是VBA的小白。
您可以鏈接Excel工作表或者您可以使用ADO。 – Fionnuala
我該怎麼辦? – Guoliangcai
看看TransferSpeadsheet。 – Fionnuala