ReDim TransVar(2 To obs, 1 To 1) 'Array for output, need to start from 2 because no diff for the first observation maybe?
This line defines an array with (obs-1) values which you have said you want index using values from 2 to obs. This explains why it only works when you index from 2 to obs, If you defined it as
Redim TransVar ( 1 to obs-1, 1 to 1)
then you would have to index from 1 to obs-1
VBa allows you to define index into an array from any number to any other number.
Try this little sub:
Sub tst()
Dim inarr() As Variant
ReDim inarr(-3 To 5, 1)
For x = LBound(inarr()) To UBound(inarr())
MsgBox x
Next x
End Sub
One other thing to notice is that I have used the two functions Lbound and Ubound to find the low and the high values on the indices. This is going to be much more reliable when you are looping through arrays that using the Application.count. because this relies on all cells being occupied
I had a look at your second "test" sub and the indexing has gone wrong. See my comments in the code: ( there may be other errors too)
Sub test()
Application.ScreenUpdating = False
Dim RawVar As Variant 'array to collect raw data
Dim endrow, endcol, lastx, lastx2, i, z, k, j, LagsReqd As Integer
' LagsReqd = 3
endrow = Range("B20000").End(xlUp).Row
lastx = Range("B5").End(xlToRight).Column '
totalvar = lastx - 2
'Step 1 copy raw range over
RawVar = Range(Cells(5, 3), Cells(endrow, lastx)).Value
Dim TransVar As Variant
' Dim lagarr As Variant
ReDim TransVar(1 To endrow - 4, 1 To lastx) ' **** this defines an aray with endrow-4 values
'ReDim lagarr(1 To endrow, 1 To CLng(LagsReqd) * lastx)
'STEP2 - create differenced variable
'Create differences of y and x variables
z = 1 'for each col
For k = 2 To endrow 'for each row '' ***** this loop goes from 2 to endrow
TransVar(1, z) = ChrW(916) & "1 Header"
'If (kk) = "" Then
'TransVar(k, z) = "yut"
TransVar(k, z) = RawVar(k, z) - RawVar(k - 1, z) ' ****this will cause an error when k gets to endrow -3 which is beyond the end of the array
Next k
' Next z
' output differences
Range(Cells(1, 1 + lastx), Cells(endrow, 2 * lastx)) = TransVar ' Transvar is only 1 to endrow - 4 so the range is the wrong size
' Range("F1:f32").Value = Application.Transpose(TransVar)
' Range("F1:f50").Value = TransVar
' now do the lags on TransVar
'=========================================
End Sub
I often define arrays starting from row, even if my data starts a few rows down. It makes indexing easier. and if the first few rows have only got text such as headers it does matter if you copy them to an array, clear the cells and then write them back. Another useful tip is to stop the macro by putting a break in it just after all the definitions and use the local window to look at all the arrays and see where the indices start and end