+ Reply to Thread
Results 1 to 4 of 4

Deleting Query Tables with a Macro

Hybrid View

dcgrove Deleting Query Tables with a... 08-15-2008, 04:20 PM
inwalkedbud this should clear the... 08-15-2008, 11:55 PM
Tom Schreiner Hi Clayton. If you are... 08-16-2008, 03:08 AM
dcgrove In an internet full of... 08-17-2008, 12:16 AM
  1. #1
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Deleting Query Tables with a Macro

    So I have a spreadsheet that I import text files to every week for three months. At the end of the three months I have to go in and delete all of the data and the query that I have set up so that I can start over for three more months. I can import them via a macro just fine, but I cannot for the life of me figure out how to get the macro I recorded to delete them to work. I have tried three different macros with no success. The code is below. I would love some help.

    The first macro I use creates a yes/no dialog box and calls the macro to delete the actual data and queries.

    Sub Macro8()
    '
    ' Macro8 Macro
    ' Macro recorded 8/15/2008 by Clayton Grove
    '
        config = vbYesNo
        ans = MsgBox("Are you sure you want to remove every Controllables Detail report you have imported?", config)
        If ans = vbYes Then Call Macro9
        If ans = vbNo Then Exit Sub
        
    End Sub
    These are the two macros I have been unsuccessfully trying to get to work.

    Sub Macro7()
    '
    ' Macro7 Macro
    ' Macro recorded 8/15/2008 by Clayton Grove
    '
    
    '
        Sheets(Array("M1 IE", "M2 IE", "M3 IE", " M1 WK 1", " M1 WK 2", " M1 WK 3", _
            " M1 WK 4", " M2 WK 1", " M2 WK 2", " M2 WK 3", " M2 WK 4", "M3 WK 1", "M3 WK 2", _
            "M3 WK 3", "M3 WK 4", "M3 WK 5")).Select
        Sheets(" M1 WK 1").Activate
        ActiveCell.Cells.Select
        Selection.ClearContents
        Sheets(Array("M1 IE", "M2 IE", "M3 IE", " M1 WK 1", " M1 WK 2", " M1 WK 3", _
            " M1 WK 4", " M2 WK 1", " M2 WK 2", " M2 WK 3", " M2 WK 4", "M3 WK 1", "M3 WK 2", _
            "M3 WK 3", "M3 WK 4", "M3 WK 5")).QueryTables.Item(11).Delete
    End Sub
    Sub Macro9()
    '
    ' Macro9 Macro
    ' Macro recorded 8/15/2008 by Clayton Grove
    '
       
        
        
       
        Application.ScreenUpdating = False
            ActiveCell.Cells.Select
        Selection.ClearContents
        Sheets("M2 IE").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Sheets("M3 IE").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Sheets(" M1 WK 1").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets(" M1 WK 2").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets(" M1 WK 3").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets(" M1 WK 4").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets(" M2 WK 1").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets(" M2 WK 2").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets(" M2 WK 3").Select
        ActiveCell.Cells.Select
        ActiveCell.Offset(-30, 0).Range("A1").Activate
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets(" M2 WK 4").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets("M3 WK 1").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets("M3 WK 2").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets("M3 WK 3").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        Sheets("M3 WK 4").Select
        ActiveCell.Cells.Select
        Selection.ClearContents
        Selection.QueryTable.Delete
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        Sheets(" Month 1 Totals").Select
    End Sub
    Here is a link to the workbook, if that will help.

    http://www.4shared.com/file/59168046...V3AWESOME.html

    Thanks for any help you can give me, because I am at a complete loss right now.

    Clayton Grove

  2. #2
    Forum Contributor
    Join Date
    10-01-2007
    Posts
    101
    this should clear the contents. still working on the querytable part.

    Sub ggg()
    
    Dim arr As Variant
    
    arr = Array("sheet1", "sheet2")
    
    For i = LBound(arr) To UBound(arr)
    
    Sheets(arr(i)).Range("a1:c5").ClearContents
    
    Next i
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-01-2008
    Location
    Cincinnati, OH
    Posts
    150
    Hi Clayton.

    If you are creating your querytables by way of code, there may be no need to save the querytable to begin with. If this is the case, just add delete after your refresh line.

    .Refresh BackgroundQuery:=False
    .Delete
    End With
    If I am wrong above...

    Work directly with the querytables instead of the ranges they reside in.

    Sub clrwkbk()
        Dim ws As Object, qt As QueryTable
    
        For Each ws In Sheets(Array("M1 IE", "M2 IE", "M3 IE", " M1 WK 1", " M1 WK 2", " M1 WK 3", _
            " M1 WK 4", " M2 WK 1", " M2 WK 2", " M2 WK 3", " M2 WK 4", "M3 WK 1", "M3 WK 2", _
            "M3 WK 3", "M3 WK 4", "M3 WK 5"))
    
            ws.Cells.ClearContents
            For Each qt In ws.QueryTables
                qt.Delete
            Next
        Next
    End Sub
    BTW. Your file gave me a chuckle. I liked the nuclear warning.
    Last edited by Tom Schreiner; 08-16-2008 at 03:12 AM.

  4. #4
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324
    In an internet full of geniuses, you guys are the real mccoy!

    Thanks for the help, and I am glad you got a chuckle out of the pictures!

    Clayton Grove

+ 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