通过 VBA 将查询从 Access 导入 Excel 并删除单个单元格
Posted
技术标签:
【中文标题】通过 VBA 将查询从 Access 导入 Excel 并删除单个单元格【英文标题】:Import query through VBA from Acces to Excel and remove single cell 【发布时间】:2015-12-21 11:14:40 【问题描述】:我尝试将查询从 Access 2013 导入 Excel 2013。但是当我尝试删除 Excelsheet 中的单个单元格时,它会删除整行,我无法删除单个单元格并将一个单元格向右移动(没有选择)。我可以在哪里更改按值复制而不是整行的 VBA 代码?
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=D:\x.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Databas" _
, _
"e Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bul" _
, _
"k Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet " _
, _
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False" _
), Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdTable
.CommandText = Array("x")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "D:\x.mdb"
.Refresh BackgroundQuery:=True
【问题讨论】:
【参考方案1】:我认为有更好的方法来做事。有关如何改进流程的一些想法,请参阅下面的链接。
http://www.accessmvp.com/KDSnell/EXCEL_Export.htm
另外,考虑使用下面的脚本,以另一种方法来改进。
Sub Rectangle1_Click()
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
'Drive code for Access
'con.ConnectionString = "DBO=C:\Temp\MyAccess.accdb:" & _
"Driver = (Microsoft Access Dirver (*.accdb));"
'Actual ConnectionString
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\MyAccess.accdb;"
'Derived ConnectionString
'In the Immediate Window, paste the following...
'?createobject("DataLinks").PromptNew.ConnectionString
'con.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=MS Access Database;DBQ=C:\Temp\MyAccess.accdb;DefaultDir=C:\Temp;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;UID=admin;"
'Open DB Connection
con.Open
Set rs.activeconnection = con
rs.Open "Select * from SharePrices"
StartRow = 3
Do Until rs.EOF
'First Field
Cells(StartRow, 4) = rs.Fields(1).Value
'Second Field
Cells(StartRow, 5) = rs.Fields(2).Value
'Third Field
Cells(StartRow, 6) = rs.Fields(3).Value
rs.movenext
StartRow = StartRow + 1
Loop
rs.Close
Set rs = Nothing
con.Close
Set con = Nothing
' RECORD A SIMPLE MACRO TO DELETE THE DATA POINT THAT YOU WANT TO ELIMINATE
End Sub
【讨论】:
【参考方案2】:这个链接对我有用,经过小改动并将 DAO 的引用添加到库中:
http://answers.microsoft.com/en-us/office/forum/office_2007-excel/import-access-query-result-into-excel-spreadsheet/4f4ba7ab-70d2-4d27-96dd-5adb1ec89a2c
【讨论】:
以上是关于通过 VBA 将查询从 Access 导入 Excel 并删除单个单元格的主要内容,如果未能解决你的问题,请参考以下文章
如何使用sql语句和vba将数据从MS-Access导入excel power查询?
VBA 将 Excel 文件从 Access 保存为 .xls