Results 1 to 1 of 1

Looping VBA Query

Threaded View

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

    Looping VBA Query

    Hello,

    Basically I have the following VBA queries that bring some data from some .dbf files into Excel

    What I’m looking to do (If possible) is align the 3rd query (CBARTOT) with the second query (CBARSUM), this itself is very easy and not a problem, then based on the value in the first column of CBARSUM (A23) sum all the columns in CBARTOT and then loop down using the value in A24 and so on until there are no more values in the A column. Now CBARSUM could contain any number between 1 & 10 records as this query updates to a variety of different CBARSUM dbf files.

    I’ve attached a test workbook if that helps

    Sub Controller()
    '
    
    '
    
        
        Dim Mailbox As String
        Dim create As Boolean
        Dim sConn As String
        Dim sSql As String
        Dim oQt As QueryTable
        Dim r As Long
        Dim StartDate As String
        Dim j(1 To 21) As Boolean
        Dim k As Integer
    
        Dim LastRow As Long
        create = True
    
      If create Then
        
        Mailbox = ActiveWorkbook.Path
      
        
    ActiveWorkbook.Sheets("Sheet1").Select
    Range("B4:AI40,A65:D86,AK4:AR277,AT4:BC236,h63,L63,bu1:bv3").ClearContents
       
      
        
        
     ' Cresult Data
     
       sConn = "ODBC;CollatingSequence=ASCII;DBQ=C:\TEMP;DefaultDir=C:\TEMP;Deleted=1;Driver={Microsoft dBase Driver (*.dbf)};DriverId=533;FIL=dBase;"
       sSql = "SELECT * FROM """ & Mailbox & """\CRESULT.DBF CRESULT"
       Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A1"), Sql:=sSql)
        oQt.AdjustColumnWidth = False
        oQt.PreserveFormatting = True
        oQt.RefreshStyle = xlOverwriteCells
        oQt.Refresh
        
        'CBarSum
        
        sConn = "ODBC;CollatingSequence=ASCII;DBQ=C:\TEMP;DefaultDir=C:\TEMP;Deleted=1;Driver={Microsoft dBase Driver (*.dbf)};DriverId=533;FIL=dBase;"
       sSql = "SELECT CODE, NAME, TYPE FROM """ & Mailbox & """\CBARSUM.DBF CBARSUM WHERE (CBARSUM.TYPE='RETAIL')"
       Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A22"), Sql:=sSql)
        oQt.AdjustColumnWidth = False
        oQt.PreserveFormatting = True
        oQt.RefreshStyle = xlOverwriteCells
        oQt.Refresh
        
        'CBarTot
        
        sConn = "ODBC;CollatingSequence=ASCII;DBQ=C:\TEMP;DefaultDir=C:\TEMP;Deleted=1;Driver={Microsoft dBase Driver (*.dbf)};DriverId=533;FIL=dBase;"
       sSql = "SELECT * FROM """ & Mailbox & """\CBARTOT.DBF CBARTOT"
       Set oQt = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A40"), Sql:=sSql)
        oQt.AdjustColumnWidth = False
        oQt.PreserveFormatting = True
        oQt.RefreshStyle = xlOverwriteCells
        oQt.Refresh
        
        
    End If
        
    
        
    End Sub
    Many Thanks
    Attached Files Attached Files

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