-1

MS Access テーブルから Excel に行をインポートする必要があります。以下の VBA マクロはまさにそれを行います。

Sub Macro1()
'
'
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
        "ODBC;DSN=MS Access Database;DBQ=C:\Documents and Settings\Administrator\My Documents\test_db.mdb;DefaultDir=C:\Documents and Setting" _
        ), Array( _
        "s\Administrator\My Documents;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
        )), Destination:=Range("A1"))
        .CommandText = Array( _
        "SELECT Table1.ID, Table1.name, Table1.id, Table1.var1, Table1.var2" & Chr(13) & "" & Chr(10) & "FROM `C:\Documents and Settings\Administrator\My Documents\test_db`.Table1 Table1" _
        )
        .Name = "Query from MS Access Database"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertEntireRows
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

MS アクセス テーブル 1

name    id    var1    var2
joe     1     23      34

マクロを 1 回実行すると、Excel に入る

name    id    var1    var2
joe     1     23      34

マクロを再度実行すると、Excel が表示されます

name    id    var1    var2    name    id    var1    var2
joe     1     23      34      joe     1     23      34

それ以外の

name    id    var1    var2
joe     1     23      34
name    id    var1    var2
joe     1     23      34

MS Access の行を既存のデータの下にある新しいデータ行として Excel にインポートするには、何を変更すればよいか分かりますか?

4

2 に答える 2

1

変化する

)), Destination:=Range("A1"))

違うことを言うこと。多分

)), Destination:= Range("A65536").End(xlUp).offset(1,0)

データのインポート方法によっては、これ以上の作業が必要になる場合があります。

于 2012-08-17T02:13:16.573 に答える
0

エンダーランドの答えは、1か月のテキストファイルをExcelにインポートし、それらを互いの末尾に追加するクエリテーブル内でうまく機能しました。コードの一部を以下に示します。ターゲット ワークブックの 2 番目のシートには、dates=filenames が含まれています。ユーザーに月と年を尋ねて、ループ内でファイル名を生成できることに気付きました。6 月、4 月、9 月、11 月 = 30 および 2 月 = 28/29 を除いて、すべて 31 です。私はそれを行います。

Dim sDate As String Dim sDataPath As String Dim i As Integer Dim mMax As Integer Dim Label_F_Name As String Dim F_name As String

sDataPath = Worksheets("D&L").Cells(1, "G").Value ' ワークブックの 2 番目のシートにある値 mMax = Worksheets("D&L").Cells(1, "D").Value ' にある値ワークブック 2 枚目

For i = 1 To mMax sDate = "A_" + CStr(Worksheets("D&L").Cells(1 + i, "A").Value) + ".csv" ' シート内の日付のリストをループ

'データ列にファイル名のラベルを付けて、データが正しいファイルからのものであることを検証できるようにします Label_F_Name = sDate + "................................." F_name = sDataPath + sDate Range("'D&L'!D5") = F_name

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" + F_name, Destination:=Range("H1048576").End(xlUp).Offset(4, 0)) ' offset for existing header
    .Name = Label_F_Name
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertEntireRows ' appends data to end of previous
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileCommaDelimiter = True
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

次は

于 2013-09-17T16:45:47.927 に答える