psatyendra
Programmer
- May 28, 2008
- 3
Hi
I am working on VBA macro's coding for Excel from last 3 months. I did a small reporting project and delivered it to client. I query data from SQL recordsets and displaying it. But it was slow while looping recordset. So copied complete data into variant variable and i loop that variant variable. I feel performance is improved. But when it went to client for beta testing, they raised performance is poor. Is there any way to improve performance.
sample code :
Function RVALUE(account_label As String, time_label As String, entity_label As String, database_label As String, scenario_label As String, analysis_label) As Double
If (ConnectionString <> "") Then
Dim value As String
Dim fval As String
Dim i As Long
Dim dbLbl As String
dbLbl = Replace(Trim(database_label), " ", "_")
If ishsfFinEmpty = True Then
loadDbData ()
End If
For i = 0 To UBound(hsfValueList, 2)
If StrConv(hsfValueList(4, i), vbLowerCase) = StrConv(account_label, vbLowerCase) Then
If StrConv(hsfValueList(1, i), vbLowerCase) = StrConv(ana_label, vbLowerCase) Then
If StrConv(hsfValueList(6, i), vbLowerCase) = StrConv(database_label, vbLowerCase) Then
If StrConv(hsfValueList(2, i), vbLowerCase) = StrConv(entity_label, vbLowerCase) Then
If StrConv(hsfValueList(3, i), vbLowerCase) = StrConv(scenario_label, vbLowerCase) Then
If StrConv(hsfValueList(5, i), vbLowerCase) = time_label Then
value = hsfValueList(0, i)
value = Format(value, "#,###,###,##0.000000")
RVALUE= value
Exit Function
End If
End If
End If
End If
End If
End If
Next
RVALUE = ""
Else
RVALUE = ""
End If
End Function
Public Sub getDBData(database_label As String)
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
cnt.Open ConnectionString
Set cmd.ActiveConnection = cnt
cmd.CommandText = "Select PFC.DataValue,PAN.AnalysisLabel, PEN.EntityLabel, PSN.ScenarioLabel, PAC.AccountLabel, PTI.Timelabel, PEN.DataBaseName FROM " & database_label & "_fact PFC inner join " & database_label & "_Analysis PAN on PFC.ANALYSISID = PAN.ANALYSISID inner join " & database_label & "_Entity PEN on PFC.EntityID = PEN.EntityID inner join " & database_label & "_Scenario PSN on PFC.ScenarioID = PSN.ScenarioID inner join " & database_label & "_Account PAC on PFC.AccountID = PAC.AccountID inner join " & database_label & "_Time PTI on PFC.TIMEID = PTI.TimeID" & whereString
cmd.CommandType = adCmdText
On Error GoTo EndFun:
cmd.Execute
rst.Open cmd
If Not rst Is Nothing Then
On Error Resume Next
If Not rst.EOF Then
If ishsfFinEmpty = True Then
hsfValueList = rst.GetRows(rst.RecordCount)
Else
Dim i As Integer
Dim j As Integer
Dim colcount As Integer
colcount = rst.Fields.count
i = UBound(hsfValueList, 2)
With rst
Dim valfld As Field
Dim anfld As Field
Dim enfld As Field
Dim scfld As Field
Dim acfld As Field
Dim tpfld As Field
Dim dbfld As Field
valfld = .Fields(0)
anfld = .Fields(1)
enfld = .Fields(2)
scfld = .Fields(3)
acfld = .Fields(4)
tpfld = .Fields(5)
dbfld = .Fields(6)
Do Until .EOF
ReDim Preserve hsfValueList(colcount - 1, i)
'For j = 0 To colcount - 1
' hsfValueList(j, i) = .Fields(j)
'Next
hsfValueList(0, i) = valfld
hsfValueList(1, i) = anfld
hsfValueList(2, i) = enfld
hsfValueList(3, i) = scfld
hsfValueList(4, i) = acfld
hsfValueList(5, i) = tpfld
hsfValueList(6, i) = dbfld
.MoveNext
i = i + 1
Loop
End With
End If
End If
On Error GoTo 0
End If
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
GoTo EndFun:
NoDataLable:
MsgBox ("Data not available for database " & database_label)
EndFun:
End Sub
Here "hsfValueList" is variant variable. if we call RVALUE funciton from Excel number of times(Ex:200,000), then this function causes poor performance. In that variant variable contains more then 200,000 records. So i need a solution to solve this.
I am working on VBA macro's coding for Excel from last 3 months. I did a small reporting project and delivered it to client. I query data from SQL recordsets and displaying it. But it was slow while looping recordset. So copied complete data into variant variable and i loop that variant variable. I feel performance is improved. But when it went to client for beta testing, they raised performance is poor. Is there any way to improve performance.
sample code :
Function RVALUE(account_label As String, time_label As String, entity_label As String, database_label As String, scenario_label As String, analysis_label) As Double
If (ConnectionString <> "") Then
Dim value As String
Dim fval As String
Dim i As Long
Dim dbLbl As String
dbLbl = Replace(Trim(database_label), " ", "_")
If ishsfFinEmpty = True Then
loadDbData ()
End If
For i = 0 To UBound(hsfValueList, 2)
If StrConv(hsfValueList(4, i), vbLowerCase) = StrConv(account_label, vbLowerCase) Then
If StrConv(hsfValueList(1, i), vbLowerCase) = StrConv(ana_label, vbLowerCase) Then
If StrConv(hsfValueList(6, i), vbLowerCase) = StrConv(database_label, vbLowerCase) Then
If StrConv(hsfValueList(2, i), vbLowerCase) = StrConv(entity_label, vbLowerCase) Then
If StrConv(hsfValueList(3, i), vbLowerCase) = StrConv(scenario_label, vbLowerCase) Then
If StrConv(hsfValueList(5, i), vbLowerCase) = time_label Then
value = hsfValueList(0, i)
value = Format(value, "#,###,###,##0.000000")
RVALUE= value
Exit Function
End If
End If
End If
End If
End If
End If
Next
RVALUE = ""
Else
RVALUE = ""
End If
End Function
Public Sub getDBData(database_label As String)
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
cnt.Open ConnectionString
Set cmd.ActiveConnection = cnt
cmd.CommandText = "Select PFC.DataValue,PAN.AnalysisLabel, PEN.EntityLabel, PSN.ScenarioLabel, PAC.AccountLabel, PTI.Timelabel, PEN.DataBaseName FROM " & database_label & "_fact PFC inner join " & database_label & "_Analysis PAN on PFC.ANALYSISID = PAN.ANALYSISID inner join " & database_label & "_Entity PEN on PFC.EntityID = PEN.EntityID inner join " & database_label & "_Scenario PSN on PFC.ScenarioID = PSN.ScenarioID inner join " & database_label & "_Account PAC on PFC.AccountID = PAC.AccountID inner join " & database_label & "_Time PTI on PFC.TIMEID = PTI.TimeID" & whereString
cmd.CommandType = adCmdText
On Error GoTo EndFun:
cmd.Execute
rst.Open cmd
If Not rst Is Nothing Then
On Error Resume Next
If Not rst.EOF Then
If ishsfFinEmpty = True Then
hsfValueList = rst.GetRows(rst.RecordCount)
Else
Dim i As Integer
Dim j As Integer
Dim colcount As Integer
colcount = rst.Fields.count
i = UBound(hsfValueList, 2)
With rst
Dim valfld As Field
Dim anfld As Field
Dim enfld As Field
Dim scfld As Field
Dim acfld As Field
Dim tpfld As Field
Dim dbfld As Field
valfld = .Fields(0)
anfld = .Fields(1)
enfld = .Fields(2)
scfld = .Fields(3)
acfld = .Fields(4)
tpfld = .Fields(5)
dbfld = .Fields(6)
Do Until .EOF
ReDim Preserve hsfValueList(colcount - 1, i)
'For j = 0 To colcount - 1
' hsfValueList(j, i) = .Fields(j)
'Next
hsfValueList(0, i) = valfld
hsfValueList(1, i) = anfld
hsfValueList(2, i) = enfld
hsfValueList(3, i) = scfld
hsfValueList(4, i) = acfld
hsfValueList(5, i) = tpfld
hsfValueList(6, i) = dbfld
.MoveNext
i = i + 1
Loop
End With
End If
End If
On Error GoTo 0
End If
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
GoTo EndFun:
NoDataLable:
MsgBox ("Data not available for database " & database_label)
EndFun:
End Sub
Here "hsfValueList" is variant variable. if we call RVALUE funciton from Excel number of times(Ex:200,000), then this function causes poor performance. In that variant variable contains more then 200,000 records. So i need a solution to solve this.