Hi,
I was wondering if I could please get some help on the following vba program.
For ease of the actual programme that I'm writing it's easier for me to use -2 as a base for my array.
Once I've populated the array I was wondering if it's possible to re-index the array to start from 1?
I thought it was something to do with the way i'm using redim preserve but any input would be greatly appreciated!
Thanks,
Nilesh
_______________________________________________________________________________________________
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
Dim arr() As Variant
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
ReDim arr(-2 To 2, -2 To 2)
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
arr(i, j) = "test"
Next j
Next i
ReDim Preserve arr(-2 To 2, -2 To 2) '<<<< Would like this to be arr(1 to 5, 1 to 5) with elements preserved
ws.Range(Cells(1, 1), Cells(5, 5)) = arr
End Sub
I was wondering if I could please get some help on the following vba program.
For ease of the actual programme that I'm writing it's easier for me to use -2 as a base for my array.
Once I've populated the array I was wondering if it's possible to re-index the array to start from 1?
I thought it was something to do with the way i'm using redim preserve but any input would be greatly appreciated!
Thanks,
Nilesh
_______________________________________________________________________________________________
Sub test()
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
Dim arr() As Variant
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
ReDim arr(-2 To 2, -2 To 2)
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
arr(i, j) = "test"
Next j
Next i
ReDim Preserve arr(-2 To 2, -2 To 2) '<<<< Would like this to be arr(1 to 5, 1 to 5) with elements preserved
ws.Range(Cells(1, 1), Cells(5, 5)) = arr
End Sub