+ Reply to Thread
Results 1 to 8 of 8

Overite Cells VBA Query Code

Hybrid View

  1. #1
    Registered User
    Join Date
    03-25-2012
    Location
    England
    MS-Off Ver
    Excel 2007 & Excel 2011 for Mac
    Posts
    79

    Overite Cells VBA Query Code

    Hi,

    I currently have the following VBA code that imports data from a Dbf file into an Excel spreadsheet but even though it clears the sheet before i run it if i run if twice it "inserts" cells shift the remaining cells to the right

    Any ideas of how to set the code to overate existing cells would be great


    Sub DataQuery()
    '
    
    '
    Application.ScreenUpdating = False
        
        Dim Mailbox As String
        Dim create As Boolean
        Dim sConn As String
        Dim sSql As String
        Dim oQt As QueryTable
        create = True
        
         Dim qt As QueryTable
         Dim WSh As Worksheet
    
         For Each WSh In ThisWorkbook.Worksheets
              For Each qt In WSh.QueryTables
                   qt.Delete
              Next qt
         Next WSh
        
        Mailbox = ActiveWorkbook.Path
        Sheets("Info").Visible = True
        ActiveWorkbook.Sheets("Info").Select
        ActiveWorkbook.Sheets("Info").Cells.ClearContents
          
        
        If create Then
      
        
     ' Allow Data
     
       sConn = "ODBC;CollatingSequence=ASCII;DBQ=C:\TEMP;DefaultDir=C:\TEMP;Deleted=1;Driver={Microsoft dBase Driver (*.dbf)};DriverId=533;FIL=dBase;"
       sSql = "SELECT DESC, RETAIL FROM """ & Mailbox & """\ALLOW.DBF ALLOW WHERE(ALLOW.DESC='PIPECLEANING')"
       Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("k1"), Sql:=sSql)
        oQt.AdjustColumnWidth = False
        oQt.PreserveFormatting = True
        oQt.Refresh
        
        
      
            Application.ScreenUpdating = True
            
        End If 'Remove if Stacking Query And add to end.
        
    End Sub
    Thanks in advance

  2. #2
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Overite Cells VBA Query Code

    Hi

    You can programmatically set the RefreshStype property - eg before your oQT.Refresh line include the line:

    oQT.RefreshStyle = xlOverwriteCells
    For testing your Regular Expression patterns, try my Regular Expression Pattern Testing add-in!

    For notes on how to use it - see here.

  3. #3
    Registered User
    Join Date
    03-25-2012
    Location
    England
    MS-Off Ver
    Excel 2007 & Excel 2011 for Mac
    Posts
    79

    Re: Overite Cells VBA Query Code

    Quote Originally Posted by Firefly2012 View Post
    Hi

    You can programmatically set the RefreshStype property - eg before your oQT.Refresh line include the line:

    oQT.RefreshStyle = xlOverwriteCells
    Thanks for this but when running the code i get an Run-Time error 1004, Cannot complete operation: A table cannot overlap with a pivot table report, query results, a table, merged cells, or an XML mapping

    Any ideas??

    Thanks

  4. #4
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Overite Cells VBA Query Code

    If you're doing this in xl2010 then usually the querytable is contained within a ListObject, so try amending your querytable delete code to:

    Dim lo As ListObject
    
    For Each WSh In ThisWorkbook.Worksheets
              For Each lo In WSh.ListObjects
                    On Error Resume Next
                    Set qt = lo.QueryTable
                    On Error Goto 0
                    If Not qt Is Nothing Then qt.Delete
              Next lo
    Next WSh

  5. #5
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Overite Cells VBA Query Code

    Ignore my previous post - I don't think it is relevant to your situation. i think all you need to do is ensure you refresh the data synchronously (rather than asynchronously as you have it at the moment). I think what is happening is that when you click it a second time, the first refresh hasn't completed meaning you end up with two query tables on the sheet. You can avoid this happening by amending your Refresh line to:

    oQt.Refresh BackgroundQuery:=False

  6. #6
    Registered User
    Join Date
    03-25-2012
    Location
    England
    MS-Off Ver
    Excel 2007 & Excel 2011 for Mac
    Posts
    79

    Re: Overite Cells VBA Query Code

    Thanks for all of these but I'm still getting cells inserted rather than overwritten when running the code more than once!!

    I've tried differnt combinations of the code but still no joy.

    Thanks anyway

  7. #7
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Overite Cells VBA Query Code

    Did you try combining?

    oQT.RefreshStyle = xlOverwriteCells
    oQt.Refresh BackgroundQuery:=False
    Also, i you have the sheet open that contains the returned data, can you open the VBE, open up the Immediate Window (Ctrl+g) and type into it:

    ?Activesheet.ListObjects.Count
    Don't omit the leading ?. Please let me know in your reply what it returns.

  8. #8
    Registered User
    Join Date
    03-25-2012
    Location
    England
    MS-Off Ver
    Excel 2007 & Excel 2011 for Mac
    Posts
    79

    Re: Overite Cells VBA Query Code

    Hi Firefly,

    I've tried the above and still no joy. When i insert the code into the Immediate window it displays 1

    Any ideas??

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1