mercredi 1 avril 2015

ReDim Multidimensional array in VBA

I have comments set up to run in and out of IBM Cognos formulas as needed.


To do so, I am putting the ranges into an array (LCogRng).


I am getting "Subscript out of range." once I get to ReDim Preserve LCogRng(1 To N, 1 To 2) As Range.


It works without Preserve, but that defeats the point.



Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range, N As Integer
Dim CogArr() As String
Dim LCogRng() As Range

Sub AddTM1()
Set wb = ActiveWorkbook
For Each ws In wb.Worksheets
ReDim CogArr(1 To 1) As String
ReDim LCogRng(1 To 1, 1 To 2) As Range
ws.Activate

For Each rng In ws.UsedRange
N = Mid(rng.Comment.Text, 3, InStr(rng.Comment.Text, ":") - 3)
cFormula = Mid(rng.Comment.Text, 5 + N, Len(rng.Comment.Text))
If CogArr(1) = "" Then
CogArr(1) = cFormula
Set LCogRng(1, 1) = rng
ElseIf UBound(CogArr) < N Then
ReDim Preserve CogArr(1 To N) As String
ReDim Preserve LCogRng(1 To N, 1 To 2) As Range 'Error row
CogArr(N) = cFormula
Set LCogRng(N, 1) = rng
End If
ElseIf InStr(rng.Comment.Text, "TM") > 0 And Len(rng.Comment.Text) <= 6 Then
N = Mid(rng.Comment.Text, 5, 2)
Set LCogRng(N, 2) = rng
End If
End If
Next rng


Any help is much appreciated.


Aucun commentaire:

Enregistrer un commentaire