VBA下标超出范围错误

时间:2015-10-30 19:36:41

标签: vba

这里的代码是下面的代码,它在2015年5月5日/ 30日正常运行。但由于某种原因,它崩溃并给我下标范围错误。

我可能已经改变了一些链接,只是试图在概念上理解错误是什么,然后调试它。代码是OLEDB连接并按月提取数据

你能看看吗

我没有得到导致错误的行,但是它发生的SOI调用。我在考虑它的连接问题所以会尝试删除OLEDB连接并重做它

 Sub Update()

    'PCAP
    Call ReplaceConnectionandRefresh1("PCAP", "zzFS - PCAP- SCRF3", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02")

    'Capital Activity Rec
    Call ReplaceConnectionandRefresh1("INVESTRAN DATA", "zzCapital Activity by Position rec - SCRF3", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02")

    'SOI
    Call ReplaceConnectionandRefresh1("SOI from JPM investran", "zz_Schedule of Investments", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02")

    'TB
    Call ReplaceConnectionandRefresh1("Sheet2", "TB Summary_BS", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02")

    MsgBox ("All Investran data tabs have been refreshed.")
 End Sub





Sub ReplaceConnectionandRefresh1(spreadsheet As Variant, DriverName As String, RWFolder As String, dbName As String, ServerName As String)

    'Sheets(spreadsheet).Visible = True
    'Sheets(spreadsheet).Select
    'Sheets(spreadsheet).Range("A1").Select
    Set lstObj = Sheets(spreadsheet).ListObjects(1)
    Set queryTbl = lstObj.QueryTable
    queryTbl.Connection = "OLEDB;Provider=ftiRSOLEDB.RSOLEDBProvider;" _
                    & "Integrated Security=" & """" & """" _
                    & ";Location=" & dbName & ";User ID=" & """" & """" _
                    & ";Initial Catalog=" & dbName & ";Data Source=" & ServerName _
                    & ";Mode=Read;Persist Security Info=True;Extended Properties="

    mycurrentvalue = """" & dbName & """"
    mycurrentvalue = mycurrentvalue & "." & """" & RWFolder & """"
    mycurrentvalue = mycurrentvalue & "." & """" & DriverName & """"

    mycurrentvalue = mycurrentvalue & " " & """"

    mycurrentvalue = mycurrentvalue & "begin date=" & Format(Range("BeginDate"), "mm/dd/yyyy") & """"
    mycurrentvalue = mycurrentvalue & " " & """"

    mycurrentvalue = mycurrentvalue & "End Date=" & Format(Range("EndDate"), "mm/dd/yyyy") & """"
    mycurrentvalue = mycurrentvalue & " " & """"

    mycurrentvalue = mycurrentvalue & "GL Begin Date=" & Format(Range("BeginDate"), "mm/dd/yyyy") & """"
    mycurrentvalue = mycurrentvalue & " " & """"

    mycurrentvalue = mycurrentvalue & "GL End Date=" & Format(Range("EndDate"), "mm/dd/yyyy") & """"
    mycurrentvalue = mycurrentvalue & " " & """"

    mycurrentvalue = mycurrentvalue & "Legal Entity=" & Range("LEID") & """"
    mycurrentvalue = mycurrentvalue & " " & """"

    mycurrentvalue = mycurrentvalue & "GL Date=" & Format(Range("EndDate"), "mm/dd/yyyy") & """"

    mycurrentvalue = mycurrentvalue & " FLAGS[/SILENT]"
    Debug.Print mycurrentvalue

    queryTbl.CommandText = mycurrentvalue
    queryTbl.Refresh BackgroundQuery:=False


End Sub

1 个答案:

答案 0 :(得分:0)

See if this code helps. I cleaned it up a little bit I think you are having an error because you do not have QueryTable object on the sheet.Option Explicit Sub Update() 'PCAP Call ReplaceConnectionandRefresh1(Sheets("PCAP"), "zzFS - PCAP- SCRF3", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02") 'Capital Activity Rec Call ReplaceConnectionandRefresh1(Sheets("INVESTRAN DATA"), "zzCapital Activity by Position rec - SCRF3", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02") 'SOI Call ReplaceConnectionandRefresh1(Sheets("SOI from JPM investran"), "zz_Schedule of Investments", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02") 'TB Call ReplaceConnectionandRefresh1(Sheets("Sheet2"), "TB Summary_BS", "Apollo", "DB_PEFS521", "TSCRLWSQLCLP162\PEFS02") MsgBox ("All Investran data tabs have been refreshed.") End Sub Sub ReplaceConnectionandRefresh1(ByRef shTable As Worksheet, _ ByVal strDriverName As String, _ ByVal strRWFolder As String, _ ByVal strDataBase As String, _ ByVal strServerName As String) Dim oQueryTbl As QueryTable Dim strConnection As String Dim strCommand As String ' Make sure you have query rables on the sheet. If Not shTable.QueryTables.Count = 0 Then ' Prepare the connection string strConnection = "OLEDB;Provider=ftiRSOLEDB.RSOLEDBProvider;" _ & "Integrated Security=" & """" & """" _ & ";Location=" & strDataBase & ";User ID=" & """" & """" _ & ";Initial Catalog=" & strDataBase & ";Data Source=" & strServerName _ & ";Mode=Read;Persist Security Info=True;Extended Properties=" ' Prepare the command strCommand = """" & strDataBase & """" strCommand = strCommand & "." & """" & strRWFolder & """" strCommand = strCommand & "." & """" & strDriverName & """" strCommand = strCommand & " " & """" strCommand = strCommand & "begin date=" & Format(Range("BeginDate").Value, "mm/dd/yyyy") & """" strCommand = strCommand & " " & """" strCommand = strCommand & "End Date=" & Format(Range("EndDate").Value, "mm/dd/yyyy") & """" strCommand = strCommand & " " & """" strCommand = strCommand & "GL Begin Date=" & Format(Range("BeginDate").Value, "mm/dd/yyyy") & """" strCommand = strCommand & " " & """" strCommand = strCommand & "GL End Date=" & Format(Range("EndDate").Value, "mm/dd/yyyy") & """" strCommand = strCommand & " " & """" strCommand = strCommand & "Legal Entity=" & Range("LEID").Value & """" strCommand = strCommand & " " & """" strCommand = strCommand & "GL Date=" & Format(Range("EndDate").Value, "mm/dd/yyyy") & """" strCommand = strCommand & " FLAGS[/SILENT]" Debug.Print strCommand ' Add the connection information and refresh. With shTable.QueryTables(1) .Connection = strConnection .CommandText = strCommand .Refresh BackgroundQuery:=False End With Else MsgBox "There is not query tables in sheet " & shTable.Name, vbOKOnly + vbCritical, "Error" End If End Sub Hope this helps :)