LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Filling an Array in VBA

Hi,

I am working on a simulation and am struggling with filling an array.
Some background
* As first step I made a simulation of 1000 loans
* As a next step I have made a calculation sheet (Sim_bond_calc) to
calculate cash flows for each mortgage (the number of cash flows is
variable (parameter ‘i’ in my macro) / max 121 rows per loan). The
sheet uses the simulated loans as a starting point (parameter ‘h’ in
my macro)
* Now I am trying to create a macro that performs the cash flow
calculation for each simulated loan and stores the output in separate
tab. Since the number of rows may grow to approximately 121.000 I have
chosen to use an array function.

I am struggling with filling the array properly:
• First I have tried to loop through the calculation sheet and write
the results to the array. This is shown in Macro 1 below. The macro
does only store the results of the last loan (number 1000)
• Next I have tried to copy all cash flows from the calculation sheet
to the array (macro 2) but whatever I try I get a run-time ‘1004’ –
Method ‘Range of object’_Global’ failed error.

I hope someone can help me!

Kind regards,

Paul


MACRO 1

Sub Sim_Cash_Flow_Projection()
Dim CellsDown As Long, CellsAcross As Integer
Dim h As Long, i As Long, j As Integer
Dim TempArray() As Variant
Dim TheRange As Range
Dim CurVal As Variant
Dim Row_ As Integer
Dim Cnt As Integer
Dim CurVal1 As Integer
Dim RowCF As Integer


'Clear previous results
Application.Goto Reference:="SIm_Bond_Range"
Selection.ClearContents

' Get dimensions
CellsDown = Sheets("Sim_loan_DB").Range("B2").Value * 121
CellsAcross = Sheets("Sim_loan_DB").Range("B4").Value

RowCF = Sheets("Sim_loan_DB").Range("B6").Value

' Redimension temporary array
ReDim TempArray(0 To CellsDown, 0 To CellsAcross)

' set worksheet range
Set TheRange = Range(Cells(2, 1), Cells(CellsDown, CellsAcross))

' Fill the temporary array

Application.ScreenUpdating = False


Sheets("Sim_bond_calc").Select
CurVal = 26
CurVal1 = 1

For h = 1 To Sheets("Sim_loan_DB").Range("B2")
Sheets("Sim_bond_calc").Range("c10").Value = h
For i = 0 To Sheets("Sim_bond_calc").Range("c18")
For j = 0 To CellsAcross
TempArray(i, j) = Cells(CurVal + i, CurVal1 + j).Value
Next j
Next i
Next h

' Transfer temporary array to worksheet
TheRange.Value = TempArray

Application.ScreenUpdating = True

End Sub

MACRO 2
Sub Sim_Cash_Flow_Projection2()
Dim CellsDown As Long, CellsAcross As Integer
Dim h As Long, i As Long, j As Integer
Dim TempArray() As Variant
Dim TheRange As Range
Dim CurVal As Variant
Dim Row_ As Integer
Dim Cnt As Integer
Dim CurVal1 As Integer
Dim RowCF As Integer


'Clear previous results
Application.Goto Reference:="SIm_Bond_Range"
Selection.ClearContents

' Get dimensions
CellsDown = Sheets("Sim_loan_DB").Range("B2").Value * 121
CellsAcross = Sheets("Sim_loan_DB").Range("B4").Value

RowCF = Sheets("Sim_loan_DB").Range("B6").Value

' Redimension temporary array
ReDim TempArray(0 To CellsDown, 0 To CellsAcross)

' set worksheet range
Set TheRange = Range(Cells(2, 1), Cells(CellsDown, CellsAcross))

' Fill the temporary array

Application.ScreenUpdating = False


Sheets("Sim_bond_calc").Select

For h = 1 To Sheets("Sim_loan_DB").Range("B2")
Sheets("Sim_bond_calc").Range("c10").Value = h
TempArray() = Range(Cells(26,
1).Resize(Sheets("Sim_bond_calc").Range("c18"), 13)).Value
Next h

' Transfer temporary array to worksheet
TheRange.Value = TempArray

Application.ScreenUpdating = True

End Sub

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling an Array kirkm[_8_] Excel Programming 2 May 19th 09 11:19 AM
Filling an Array Quickly Bill Martin[_3_] Excel Programming 13 May 1st 09 02:47 PM
Filling an Array Quickly Bill Martin[_3_] Excel Programming 4 April 26th 09 09:26 PM
Filling an array with ranges Greg Excel Programming 1 November 12th 07 09:24 PM
filling a form with an array JT Excel Programming 1 October 26th 05 04:11 PM


All times are GMT +1. The time now is 06:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"