|
Sub 导入数据()
Dim t, s, conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Dim dz As String
Dim arr As Variant
Dim brr As Variant
Dim i As Long, m As Long, N As Long
Application.ScreenUpdating = False
s = Timer
Set t = Application.FileDialog(msoFileDialogFilePicker)
t.AllowMultiSelect = False
If t.Show = -1 Then
dz = t.SelectedItems(1)
' 使用ADODB连接读取Excel文件
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dz & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
sql = "SELECT * FROM [Sheet1$]"
Set rs = conn.Execute(sql)
' 将数据读入数组
arr = rs.GetRows
arr = Application.Transpose(arr)
rs.Close
conn.Close
N = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
ReDim brr(1 To UBound(arr), 1 To UBound(arr, 2))
For i = 2 To UBound(arr)
m = m + 1
brr(m, 1) = Val(arr(i, 1))
brr(m, 2) = arr(i, 2)
brr(m, 3) = arr(i, 3)
brr(m, 4) = arr(i, 4)
Next i
Sheet1.Cells(N + 1, "A").Resize(UBound(brr), UBound(brr, 2)) = brr
MsgBox "提取完毕" & "耗时" & Round(Timer - s, 4) & "秒"
End If
End Sub
如果文件格式允许,还可考虑使用文本文件导入
|
|