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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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