错误 1004 无法从 WorksheetFunction 获取 Index 属性
我有点被这个任务困住了。我有一个宏,可以从两个不同的工作簿中检索要处理的两个数据范围,并将它们填充到两个变体中。收集成功,因为我可以在本地窗口中调试和检查它们,其中一个是 data1(变体 1 到 79),另一个是 data2(变体 1 到 10)。这就是我获取 data1 的方式
With wbExterno.Sheets(1)
data1 = .Range(tmpStr + ":" & ToolboxMod.Number2Char(lastCol) & lastRow).Value
End With
现在我想将 data1 中的相关元素复制到一个新数组,将其命名为 Dim newData as Variant
。我已经检查in 所以关于这个,这就是我得到的
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."
它会引发错误 1004 无法获取来自工作表函数的索引属性。我在这里做错了什么?我应该将输入过滤到 data1 中吗?这样比较容易吗?快点?
编辑:我已在具有相同数据(无 VBA)的工作表中尝试过该方法,但它仅适用于翻译后的方法名称(=Index()西班牙语为 =Indice() )。除此之外,它还有效。但是,尝试将 data1 设置为范围和变体并且......它不起作用。 另外,我检查了断点, data1 是 Variant/Variant(78) ,每个元素 (data1(i) ) 都是包含每行的 Variant (1 To 8)
EDIT 2< /strong>:在答案中建议的测试之后,我尝试了一种更老式的方法:
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
它实际上逐个字段设置新数组中我需要的行。我将不回答这个问题;也许我们可以找到更好的方法。
解决方案我最终使用的代码是这样的:
' 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
...还有一些与此无关的代码,然后
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我有时间去看看。如果您的
Index
函数没有正确的参数,那么它将抛出错误 1004
。假设您的范围是单个单元格,那么当您在第 2 列中查找值时,它不会出现任何内容(即,错误 1004
)。但第 1 列第 1 行将返回正确的结果。因此,在您的代码中,请确保没有超出变量
i
。所以这就是您的代码中的错误:应该如此
,因此它不会超过数组中的列数。
OK, I had time to look at it. If your
Index
function doesn't have the correct parameters then it will throw theerror 1004
. Let's say if your range is a single cell, then when you look for a value in column 2, it won't come up with anything (i.e.,error 1004
). But column 1 row 1 will return a correct result.So in your code make sure that the variable
i
is not being exceeded. So this is what is wrong in your code:Should be
So it wont exceed the number of columns in your array.
正如 Remou 指出的,第一个参数应该是一个范围。
但是,除了删除 .value 之外,您还需要稍微修改代码。
data1 需要调暗为 Range 并使用 Set 关键字进行分配,如下所示:
As Remou pointed out, the first argument should be a range.
However, youll need to slightly modify your code beyond just removing the .value.
data1 will need to be dim'd as a Range and assigned with the Set keyword like so: