0

I have 3 variant arrays: Array1 Array2 Array3

I am wanting to add the counter value of my for loop to the end of the array name to save repeating the same code line over e.g. :

For i = 1 To oXlWkBk.Sheets.Count
FillArray ArraySource & i, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Next i

instead of:

For i = 1 To oXlWkBk.Sheets.Count
FillArray ArraySource1, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)

FillArray ArraySource2, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)

FillArray ArraySource3, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Next i

I've tried using:

  • Array & i
  • Array & Cstr(i)
  • Array(i)

Is it possible to append the counter value as a suffix to complete the array name?

2
  • Nope. Put them in a Scripting.Dictionary instead. Then you can use string keys. Commented Jan 27, 2017 at 19:33
  • 1
    ...or put your arrays in an array and use the numeric index Commented Jan 27, 2017 at 19:47

2 Answers 2

1

This should work:

Dim Arrays As Variant
Arrays = Array(Array1, Array2, Array3)

For i = 1 To oXlWkBk.Sheets.Count
    FillArray Arrays(i-1), "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Next i

Since Array() returns a 0-based array you need to be careful with the indices.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. When I try this I get a compile error: Type Mismatch: Array or user-defined type expected. Not sure why this as I already have the Arrays array declared via Public Arrays() as Variant
@BLothian Drop the () and declare it as Arrays As Variant rather than Arrays() As Variant. You don't need to declare Arrays as an array -- just declare it as a simple variant variable. There is a difference between a variant which happens to contain an array and an array of variants. Along the same lines, if there is still a problem you could change the declaration of FillArray so that its first argument is a simple variant and not anything more specific, although this is less likely to be the issue.
0

Can you try to store all three arrays inside an array and then reference the containing array with the i? For example,

Sub test()
largerArray = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9))
For i = 0 To 2
    Debug.Print (sumArray(largerArray(i)))
Next i
End Sub
Function sumArray(thisArray As Variant) As Integer
sumArray = 0
For i = LBound(thisArray) To UBound(thisArray)
    sumArray = sumArray + thisArray(i)
Next i
End Function

So in your case, you would have

largerArray=Array(ArraySource1, ArraySource2, ArraySource3)
for i=0 to oXlWkBk.Sheets.Count
    FillArray largerArray(i), "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)

etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.