使用VBA从Access导出部分列到Excel

问题描述 投票:-2回答:1

我正在尝试从下面的代码中导出部分或部分列。

它正在导出从Access到Excel的所有内容。

我对任何建议表示赞赏。

Dim theFilePath As String
reportname = "X"
theFilePath = "C:\Documents and Settings\" & Environ("UserName") & "\Desktop\"
theFilePath = theFilePath & reportname & "_" & Format(Date, "yyyy-mm-dd") & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, reportname, theFilePath, True
MsgBox "The Excel file is Downloaded in your Desktop"
access-vba
1个回答
0
投票

构造一个查询,从您的表/查询中选择所需的列(在代码中命名为X),并使用TransferSpreadsheet对象的DoCmd方法输出查询。

例如,您可以构造一个如下所示的查询,其中YourField1...N是所需的列,x是您的数据集(表/查询):

select x.YourField1, x.YourField2, x.YourField3 from x

将此查询保存在MS Access中作为MyQuery,然后将reportname = "X"更改为reportname = "MyQuery"

© www.soinside.com 2019 - 2024. All rights reserved.