简体   繁体   中英

Import query through VBA from Acces to Excel and remove single cell

I try to import a query from Access 2013 to Excel 2013. But when I then try to remove a single cell in the Excelsheet it removes the whole row and I can't remove a single cell and shift one to the right (don't get the option). Where can I change in the VBA code that is copies per value and not the whole rows?

    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

I think there is a better way to do thing. Please see the link below for some ideas of how to improve your process.

http://www.accessmvp.com/KDSnell/EXCEL_Export.htm

Also, CONSIDER USING THE SCRIPT BELOW, for another way to improve things.

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

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