Hello,
About a year ago I used the VBA code posted by Taylor in the forum in 2015, which I managed to get to work (although slowly) in exporting all scenario energy consumption results from all branches. The link to the original code I used:
https://www.energycommunity.org/default.asp?action=9&fid=22&TID=3431
However, I have tried using this code again on a new project, and now the code throws some errors.
The code I am using right now is slightly modified from that original post so as to exclude current accounts, my code (between the hashes):
######################
If Not IsObject(LEAP) Then
set LEAP = CreateObject("Leap.LEAPApplication")
End If
dim tech_branches
set tech_branches = CreateObject("Scripting.Dictionary")
for each b in LEAP.Branches
if b.Branchtype = 4 then
tech_branches.Add b.BranchID, b.Name
end if
next
set xla = GetObject(,"Excel.Application")
set xls = xla.sheets.add
xls.name = "ATestNameOfSheet"
LEAP.Verbose = 4
LEAP.Visible = true
' Add headers to Excel worksheet '
r = 1
xls.Cells(r, 1).Value = "Scenario"
xls.Cells(r, 2).Value = "Year"
xls.Cells(r, 3).Value = "Branch"
xls.Cells(r, 4).Value = "Fuel"
xls.Cells(r, 5).Value = "Value (GJ)"
' Loop through scenarios, years, branches and export final energy demand in gigajoules '
Print("Starting loop")
for each s in LEAP.Scenarios
If s<> "Current Accounts" Then
for y = LEAP.BaseYear to LEAP.EndYear
for each key in tech_branches.keys
r = r + 1
xls.Cells(r, 1).Value = s
xls.Cells(r, 2).Value = y
xls.Cells(r, 3).Value = LEAP.Branch(key).FullName
xls.Cells(r, 4).Value = LEAP.Branch(key).Fuel.Name
xls.Cells(r, 5).Value = LEAP.Branch(key).Variable("Energy Demand Final Units").Value(y, "Gigajoule")
next
next
End If
next
xla.screenupdating = true
xla.visible = true
######################
The errors that are thrown are:
"LEAP Automation error
Branches.Item: Invalid Index (298) Must be in range 1 – 76"
this happens twice, and then a third error:
"Object Required: LEAP.Branch(…).Fuel"
These errors are in reference to the last 3 lines of the loop print the values to excel cells.
I'm not great at VBA, I know my way around with a bit of googling help, but I have not been able to get this script working.
I did try a newer posted script, also by Taylor, from:
https://www.energycommunity.org/default.asp?action=9&tid=3729&fid=22
and get the same thrown errors.
Any help please with fixing the code to export results would be great, thanks!
-Bryce