Exporting query to existing worksheet in existing workbook. ERROR "Subscript out of range"

hasasl

New Member
Joined
Apr 17, 2015
Messages
1
I have to export a query (the entire table) to a existing workbook which has a worksheet which needs to be overwritten. Why do I get this error?


Code:
        Dim rst As Recordset
        Dim ApXL As Object
        Dim xlWBk As Object
        Dim xlWSh As Object
        Dim fld As DAO.Field
        Dim lngMaxRow As Long
        Dim lngMaxCol As Long
    
        Dim strPath As String
    
        Const xlCenter As Long = -4108
        Const xlBottom As Long = -4107
    
        On Error GoTo Err_Handler
    
            strPath = CurrentProject.Path & "\Qry_V.xlsx"
                    
            Set rst = CurrentDb.OpenRecordset("Qry_V")
            Set ApXL = CreateObject("Excel.Application")
        
        
            Set xlWBk = ApXL.Workbooks.Open(strPath)
                
            Set xlWSh = xlWBk.Worksheets(Qry_V)
        
            rst.MoveFirst
            xlWSh.CopyFromRecordset rst
            xlWSh.SELECT
            
        
        'xlWSh.Activate
        'xlWSh.Cells.rows(7).AutoFilter
        'xlWSh.Cells.rows(7).EntireColumn.AutoFit
    
        rst.Close
        Set rst = Nothing
        'Remove prompt to save file
        ApXL.DisplayAlerts = False
        xlWBk.SaveAs CurrentProject.Path & "\Qry_V.xlsx", 51
        ApXL.DisplayAlerts = True
        'Open after report is completes
        ApXL.Visible = True
        'ApXL.Quit
    
        Exit Sub
Err_Handler:
        DoCmd.SetWarnings True
        MsgBox Err.Description, vbExclamation, Err.Number
        Exit Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
In the visual basic editor, click the grey column to the left of the code to put a code stop on one of the lines. Use the earliest non-declaration line, so On Error GoTo Err_Handler, or the next one if that doesn't work. Then run the code the normal way. The run will stop at the code stop and you can use F8 to advance one line at a time. This will help you to find which line is giving the error. You may even want to turn off error handling for the test.
 
Upvote 0

Forum statistics

Threads
1,221,877
Messages
6,162,583
Members
451,776
Latest member
bosvinn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top