Sub ImportExcelToAccess()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim fld As ADODB.Field
Dim strExcelPath As String
Dim strSQL As String
' Excel ファイルのパスを設定
strExcelPath = "C:\path\to\your\excel.xlsx"
' Excel への接続文字列 (Excel バージョンによって異なる場合がある)
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strExcelPath & ";" & _
"Extended Properties='Excel 12.0 Xml;HDR=YES';"
conn.Open
' Excel ファイルの特定のシートと範囲からデータを選択するSQLクエリ
strSQL = "SELECT * FROM [Sheet1$]"
' クエリを実行して結果を Recordset に格納
rs.Open strSQL, conn, adOpenStatic, adLockReadOnly
' 結果をループして処理(例: デバッグ出力にフィールド名と値を表示)
Do While Not rs.EOF
For Each fld In rs.Fields
Debug.Print fld.Name & ": " & fld.Value
Next fld
rs.MoveNext
Loop
' オブジェクトを閉じてクリーンアップ
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
End Sub
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme