I am going slightly crazy on this and I'm not sure if it's because I'm searching for something that isn't possible or I'm just not searching in the right places! I am trying to use a for loop to create a series of data, assign that data and the index number to an array, and then send that array to a worksheet.
I've been trying to adopt so many different bits of code from youtube tutorials and forum posts that I cannot truly figure out where I went wrong. Right now the array sends only the data and not the index number to the array. I'm sure it's something simple but I've just stared at it for too long and can't figure it out. Any help is greatly appreciated.
Option Explicit Sub MonthlyObligation() Dim ws As Worksheet Set ws = Sheets("Page 1") ' 1. Define the array Dim arr() As Variant ' 2. Generate Data with for Loop ' 2a. Determine the number of rows in the array Dim BaseTerm As Integer Dim OptionCount As Integer Dim OptionTerm As Integer BaseTerm = ws.Range("LeaseTerm").Value OptionCount = ws.Range("RenewalOptions").Value OptionTerm = ws.Range("TermPerOption").Value Dim TotalTerm As Long TotalTerm = ((BaseTerm + (OptionCount * OptionTerm)) / 12) ' 2b. Redefine the array to the row count ReDim arr(1 To TotalTerm, 1) ' 2c. Create Data for the array Dim MonthlyBaseRent As Double, AnnualRiser As Double, currentRow As Long MonthlyBaseRent = ws.Range("BaseRent").Value AnnualRiser = ws.Range("RentIncrease").Value Dim i As Long, j As Long For i = 1 To TotalTerm For j = 1 To 1 MonthlyBaseRent = (MonthlyBaseRent * (1 + AnnualRiser)) ' 2a. Store the Data in the Array arr(i, j) = MonthlyBaseRent Debug.Print arr([i], [j]) Next j Next i End Sub
i
?ReDim arr(1 To TotalTerm, 1)
is the same asReDim arr(1 To TotalTerm, 0 To 1)
- is that what you want? If you want to store two values pr row thenReDim arr(1 To TotalTerm, 1 To 2)
might be better.For j = 1 To 1
. If this is deliberate, it is totally unnecessary, as it executes only once. This would also mean that a single dimensional array would suffice.