0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Acsessでエクセルを外部テーブルとしてインポートする方法

Posted at
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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?