Hi,
I am working on a workbook in which I have to do some rather complicated formulas with arrays and ranges, it slowed excel down to the point that calculations took over half an hour. Hence, I wanted to do the calculations in vba.
I have an array, which I fill with ranges. so say:
Throughput Out = {A1:B2 ; A4:B5; A7:B8}
Now I want to multiply this with a range in another sheet (A1:B2). I'm getting a type mismatch when I try to multiply the ranges in the array with the range in the other sheet:
Below my full code:
Code
'Create Arrays
Dim ThroughputOut(0 To 23) As Variant
Dim ThroughputIn(0 To 23) As Variant
Dim AmountPaid(0 To 23) As Variant
Dim AmountReceived(0 To 23) As Variant
'Create Ranges
Dim DailyOutflow As Range
Dim DailyInflow As Range
Dim LastCol As Long
'Set Scenario Names
Call SetScenarios
'Loop through scenarios
For Each Item In Scenarios()
Set WbScenarios = Workbooks.Open(ActiveWorkbook.Path & "\" & Item & ".xlsx")
'Set worksheets
Call SetSheets
'Set Amount of Days in Month
Call CountDays
'Find last column filled with currency
LastCol = ShtPayments.Cells( _
Application.Match( _
StrPaymentsOut, _
ShtPayments.Range("b:b"), 0), ShtPayments.Columns.Count).End(xlToLeft).Column
'Fill Ranges with Source Data for each scenario
For x = 0 To 23
ThroughputOut(x) = ShtThroughputOut.Range( _
ShtThroughputOut.Cells( _
Application.Match( _
StrThroughput & Format(x, "00") & ":00 (CCY timeframe)", _
ShtThroughputOut.Range("b:b"), 0) + 2, 3), _
ShtThroughputOut.Cells( _
Application.Match( _
StrThroughput & Format(x, "00") & ":00 (CCY timeframe)", _
ShtThroughputOut.Range("b:b"), 0) + 1 + DaysInMonth, LastCol))
ThroughputIn(x) = ShtThroughputIn.Range( _
ShtThroughputIn.Cells( _
Application.Match( _
StrThroughput & Format(x, "00") & ":00 (CCY timeframe)", _
ShtThroughputIn.Range("b:b"), 0) + 2, 3), _
ShtThroughputIn.Cells( _
Application.Match( _
StrThroughput & Format(x, "00") & ":00 (CCY timeframe)", _
ShtThroughputOut.Range("b:b"), 0) + 1 + DaysInMonth, LastCol))
Next x
Set DailyOutflow = ShtPayments.Range( _
ShtPayments.Cells( _
Application.Match( _
StrPaymentsOut, _
ShtPayments.Range("b:b"), 0) + 2, 3), _
ShtPayments.Cells( _
Application.Match( _
StrPaymentsOut, _
ShtPayments.Range("b:b"), 0) + 1 + DaysInMonth, LastCol))
Set DailyInflow = ShtPayments.Range( _
ShtPayments.Cells( _
Application.Match( _
StrPaymentsIn, _
ShtPayments.Range("b:b"), 0) + 2, 3), _
ShtPayments.Cells( _
Application.Match( _
StrPaymentsIn, _
ShtPayments.Range("b:b"), 0) + 1 + DaysInMonth, LastCol))
'multiply Throughput Out with Daily Outflow and Throughput In with Daily Inflow
For x = 0 To 23
ThroughputOut(x) = ThroughputOut(x) * DailyOutflow
ThroughputOut(x) = ThroughputIn(x) * DailyInflow
Next x
WbScenarios.Close
Next Item
Display More