I'm kinda stuck with this task. I have a macro that retrieve two ranges of data to process from two different workbooks and stuff them into two Variant. The gathering is successful as I can debug and inspect them in Local window, one being data1(variant 1 to 79) and other data2(variant 1 to 10). This is how I get data1
With wbExterno.Sheets(1)
data1 = .Range(tmpStr + ":" & ToolboxMod.Number2Char(lastCol) & lastRow).Value
End With
Now I want to copy relevant elements from data1 to a new array, name it Dim newData as Variant
. I've already checked in SO about this and this is what I got
Dim filterCount As Integer
counter = 0
filterCount = 1
' Para cada elemento en el array...
For i = 1 To UBound(data1)
'Comparar el campo fecha...
tmpTest = data1(i, 1)
' ...con la fecha del ejercicio
If (comparacionActual.FechaEjercicio = tmpTest) Then
'MsgBox "iguales!"
'se crea un array filtrado con los elementos pertinentes
filter1(filterCount) = Application.WorksheetFunction.Index(data1, 0, i)
PlusOne filterCount 'this is a custom function that increments in 1
End If
Next
' se informa el resultado del filtrado
MsgBox "Copied: " & filterCount & " rows."
It raises a Error 1004 unable to get Index property from worksheetfunction. What am I doing wrong here? Should I filter the input into data1 instead? Easier that way? Faster?
EDIT: I've tried the method in a worksheet with the same data (no VBA) and it only worked with the translated method name ( =Index() in spanish is =Indice() ). Other than that, it worked. But, tried setting data1 as range and variant and... it didn't work. Also, I checked with a breakpoint and data1 is a Variant/Variant(78) and each element (data1(i) ) is a Variant (1 To 8) containing each row
EDIT 2: After a test suggested in the answers, I tried a more old-school approach:
With wbInterno.Sheets(1)
data1 = .Range(tmpStr + ":" & ToolboxMod.Number2Char(lastCol) & lastRow).Value
filter1 = wbMe.Worksheets.Add.Range(tmpStr + ":" & ToolboxMod.Number2Char(lastCol) & lastRow).Value
End With
'... some other stuff
' Field by field
For j = LBound(data1, 2) To UBound(data1, 2)
'MsgBox "check" & data1(i, j)
filter1(filterCount, j) = data1(i, j)
Next
And it actually sets field by field, the rows I need in the new array. I'll leave the question unanswered; perhaps we can find a better way.
Solution The code I use in the end is this one:
' Define array with a range. Initialize destination array with the same size.
data1 = .Range(tmpStr + ":" & ToolboxMod.Number2Char(lastCol) & lastRow).Value
filter1 = wbMe.Sheets("tmp").Range(tmpStr + ":" & ToolboxMod.Number2Char(lastCol) & lastRow).Value
... some more code not relevant to this and then
Dim tmpTest As Variant
Dim filterCount As Integer
filterCount = 1
' integer used for presentation only
conteoRegistros = 0
' for each element in array...
For i = 1 To UBound(data1)
'Compare a certain field...
tmpTest = data1(i, 1)
' ...with some other variable. If so...
If (comparacionActual.FechaEjercicio = tmpTest) Then
'...copy column by column into new
For j = LBound(data1, 2) To UBound(data1, 2)
'MsgBox "check" & data1(i, j)
filter1(filterCount, j) = data1(i, j)
Next
PlusOne filterCount
PlusOne conteoRegistros
End If
Next