简体   繁体   中英

Importing data from Access into Excel

I have a macro in an Excel budget worksheet I am working on that pulls an entire table from an Access database. My macro then has a bunch of SUMIFS and formatting functions that organize and format specific data into a separate worksheet. I would prefer that the entire table (21,000 lines of budget data) not be dumped into the Excel worksheet when I only require a few lines of data depending on which "team" I am selecting. Is there any way to specify in my Access data dump that I only want specific lines of data from the table (of 21,000 lines) rather than the entire table? Is there a way to ignore the headers when doing the data dump?

Thanks in advance!

If you've recorded a macro doing this, you can simply edit the commandtext to be your select statement. Example below would only pull a single column, ID, from the Templates table, instead of the whole table.

Sub MyRecordedMacro
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
    "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Users\meuser\Desktop\database.accdb;Mode=Share Deny " _
    , _
    "Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:En" _
    , _
    ...
    .CommandText = "Select ID From Templates"
    ...
    ...
    End With

You can learn how to create an appropriate select statement here: http://www.w3schools.com/sql/sql_select.asp

I don't know how to exclude headers from this type of import. However you could just delete the header row in excel after the import.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM