Subscript out of range error in VBA with Table formula

codeliftsleep

Board Regular
Joined
Apr 14, 2017
Messages
103
Having a very annoying problem for users which does not happen to me.

I run code when the workbook is opened that automatically connects to a SQL Server and brings back data for users depending on their role. The connections work without issue for me, but at times they throw errors for other users when trying to update a formula for a table:

Code:
Private Sub CopyDailyCases(StartDate As String, EndDate As String)
    Dim DailyResolved As Worksheet
    Dim DailyPending As Worksheet
    Dim YTDResolved As Worksheet
    Dim YTDPending As Worksheet
    Dim Table As ListObject
    Dim Header As ListColumn
    Dim iCol As Long
    
    Set YTDResolved = Sheets("YTD Resolved Cases")
    Set YTDPending = Sheets("YTD Pending Cases")
    Set DailyResolved = Sheets("Resolved Cases")
    Set DailyPending = Sheets("Pending Cases")
    
    Set Table = YTDResolved.ListObjects("Table_YTDResolvedCases")
    
    With Table
        YTDResolved.AutoFilterMode = False
        'On Error GoTo DoesNotExist
        .ListColumns("Fixed_Report_Date").DataBodyRange.FormulaR1C1 = "=INT([SUB_QUERY_CLOSED_ON])" <---- ERROR Happens RIGHT HERE
        iCol = .ListColumns("Fixed_Report_Date").Index
        .Range.AutoFilter Field:=iCol, Criteria1:=">=" & CDate(StartDate), Operator:=xlAnd, Criteria2:="<=" & CDate(EndDate)
        'Copy the date range
        .Range.SpecialCells(xlCellTypeVisible).Copy Destination:=DailyResolved.Range("A1")
        YTDResolved.ShowAllData
        YTDResolved.AutoFilterMode = False
    End With

Now, I know that if there is no data in the table this will throw an error, but everytime I look the data seems like it is there already when the error occurs. It almost has to be because this query runs first and then 3 additional queries run after it, which take longer to finish than this one does. The frustrating part is that it never happens when I run it, only when other people run it. The table name is valid, the column name is valid, what can be the cause of this to keep happening?

In the off chance the users are having this happen because the data hasn't finished loading for some reason, what is
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You can pause a few seconds before calling CopyDailyCases or before calling CopyDailyCases put the DoEvents instruction or both.

eg. 3 second pause
Code:
Application.Wait Now() + TimeValue("00:00:03")
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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