+ Reply to Thread
Results 1 to 1 of 1

Issue managing ListObjects and Queries

Hybrid View

devon_a_b Issue managing ListObjects... 08-24-2021, 07:56 AM
  1. #1
    Registered User
    Join Date
    08-24-2021
    Location
    United States
    MS-Off Ver
    Excel 2008
    Posts
    1

    Issue managing ListObjects and Queries

    --EDIT--

    I believe I found the issue, it seems that I was attempting to unlist the query after deleting without referencing the proper worksheet for the ListObject. Must have got it from some example code I looked at and I didn't fully understand the functionality. However, if anyone has tips to make this a little cleaner I would greatly appreciate it.


    I'm fairly new to coding in VBA but I have some experience with Python and very limited C++ experience, so I understand some basic concepts. I tried building this macro to manage a workbook that allows me to view specific trailers, gathering the data from a spreadsheet that's exported from a Microsoft Access Database. It works well, if I run it about 3 times...but for the first couple of times I get a 'subscript out of range' error.


    The following is my code. I know it's probably not the prettiest. I tried piecing it together with google-fu and recording/reviewing macros. Any assistance is appreciated.

    Sub Setup()
    
    Dim q1_exists As Boolean
    Dim q2_exists As Boolean
    Dim old_exists As Boolean
    Dim table_ As String
    
    
    
    
        ' Cycle through worksheets to check for "old"
        ' If "old" exists delete it
        
        For o = 1 To Worksheets.Count
            If Worksheets(o).Name = "old" Then
            old_exists = True
            End If
        Next o
        
        If old_exists Then
            Sheets("old").Delete
        End If
    
        ' Cycle through worksheets to check for "current"
        ' If "current" exists change name to "old"
            
        For i = 1 To Worksheets.Count
            If Worksheets(i).Name = "current" Then
            Sheets("current").Name = "old"
            End If
        Next i
        
        ' Check if query "rec_" exists
        
        For q2 = 1 To ActiveWorkbook.Queries.Count
            If ActiveWorkbook.Queries(q2).Name = "rec_" Then
                q2_exists = True
                
            End If
        Next q2
        
        ' If "rec_" exists delete it
        
        If q2_exists Then
            ActiveWorkbook.Queries("rec_").Delete
            ActiveSheet.ListObjects("rec_").Unlist
            'ActiveSheet.ListObjects("rec_").Name = "old"
            
            
            
        End If
        
        
    
    
    ' Refresh initial query
    '
        ActiveWorkbook.Connections("Query - z").Refresh
    
    
    ' Create new worksheet and export table data to new worksheet
        
        ' Check if query "rec" already exits
        
        For q = 1 To ActiveWorkbook.Queries.Count
            If ActiveWorkbook.Queries(q).Name = "rec" Then
            
                q1_exists = True
                
            End If
        Next q
            
    
            
        ' If "rec" does not exist create query named rec
        
        If Not q1_exists Then
            
            ActiveWorkbook.Queries.Add Name:="rec", Formula:= _
                "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.CurrentWorkbook(){[Name=""z""]}[Content]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Trailer"", type text}, {""SCAC"", type text}, {""Status"", Int64.Type}, {""Text20"", type text}, {""Comment"", type text}, {""Update Time"", type datetime}, {""Yard Loc"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
            ActiveWorkbook.Worksheets.Add
            
            ActiveSheet.Name = "current"
        
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=rec;Extended Properties=""""" _
            , Destination:=Range("$A$1")).QueryTable
            .CommandType = xlCmdSql
            .CommandText = Array("SELECT * FROM [rec]")
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "rec"
            .Refresh BackgroundQuery:=False
        End With
        
        table_ = "rec"
        
        
            
                
        ' If "rec" exists create query named rec_
                
        Else
            
            ActiveWorkbook.Queries.Add Name:="rec_", Formula:= _
                "let" & Chr(13) & "" & Chr(10) & "    Source = Excel.CurrentWorkbook(){[Name=""z""]}[Content]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Trailer"", type text}, {""SCAC"", type text}, {""Status"", Int64.Type}, {""Text20"", type text}, {""Comment"", type text}, {""Update Time"", type datetime}, {""Yard Loc"", Int64.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
            ActiveWorkbook.Worksheets.Add
            
            ActiveSheet.Name = "current"
        
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=rec_;Extended Properties=""""" _
            , Destination:=Range("$A$1")).QueryTable
            .CommandType = xlCmdSql
            .CommandText = Array("SELECT * FROM [rec_]")
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "rec_"
            .Refresh BackgroundQuery:=False
        End With
            
        End If
        
            
        
        
       
        
    
    
    
    
    
    
    
    
    End Sub
    Attached Files Attached Files
    Last edited by devon_a_b; 08-24-2021 at 11:52 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Need help on Managing Order, receipt and Issue - can anyone provide proven template!!!
    By jsano.g in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-21-2014, 05:03 AM
  2. Me.ListObjects("tblName").DataBodyRange.Rows.Delete - Does Me Not Like ListObjects?
    By snapfade in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-30-2014, 01:56 PM
  3. [SOLVED] ListObjects Print Area Issue - There is no headings
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-28-2013, 08:20 PM
  4. Replies: 0
    Last Post: 01-21-2013, 12:47 PM
  5. Embedded Access queries in Excel - if the database moves, how to update queries?
    By Paul_mcc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-06-2012, 07:52 PM
  6. ListObjects
    By costadina in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2009, 08:17 AM
  7. [SOLVED] ListObjects.Add error
    By doug in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-06-2006, 10:45 AM

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