简体   繁体   English

将.Find()函数结果保存到数组Excel VBA

[英]Save .Find() function results to array Excel VBA

I'd like to save all found numbers of rows containing the string into the array. 我想将包含字符串的所有找到的行数保存到数组中。 I've written a function which works properly but I can't save its results into array. 我编写了一个功能正常的函数,但是无法将其结果保存到数组中。 I might do it saving not as .Row values but .Address as strings however it's not my goal. 我可能不会将它保存为.Row值,而是将.Address保存为字符串,但这不是我的目标。 I tried change array type from integer to long, but that does not work. 我尝试将数组类型从整数更改为long,但这不起作用。 Have u any idea what i should to do? 你知道我该怎么办吗? What is the type of value .Row? 值行的类型是什么? By manual .Find() function return Range type, but I'm not sure that .Row value is also Range. 通过手动.Find()函数返回Range类型,但是我不确定.Row值是否也是Range。 If it is how to use this knowledge to solve the problem? 如果是如何使用这些知识来解决问题? My code: 我的代码:

Function findTesterRows(przypisFileName As String, testerName As String, tableSize As Integer) As Integer()
Dim tableRange As String
    tableRange = "A2:L" & tableSize
Dim myArray() As Integer
Dim i As Integer
    i = 0

With Workbooks(przypisFileName).Worksheets("Sheet1").Range(tableRange)
    Set foundRow = .Find(What:=testerName, LookIn:=xlValues)
    If Not foundRow Is Nothing Then
        firstAddress = foundRow.Address
        Do
            i = i + 1
            Set foundRow = .FindNext(foundRow)
        Loop While Not foundRow Is Nothing And foundRow.Address <> firstAddress
    End If
End With

ReDim myArray(0, i)
i = 0

 With Workbooks(przypisFileName).Worksheets("Sheet1").Range(tableRange)
    Set foundRow = .Find(What:=testerName, LookIn:=xlValues)
    If Not foundRow Is Nothing Then
        firstAddress = foundRow.Address
        Do
            MsgBox foundRow.Row
            myArray(i) = foundRow.Row '!!HERE IS THE PROBLEM!!
            i = i + 1
            Set foundRow = .FindNext(foundRow)
        Loop While Not foundRow Is Nothing And foundRow.Address <> firstAddress
    End If
End With

findTesterRows = myArray

End Function

Check out how you redimensioned the array to ReDim myArray(0,i) . 查看如何将数组重新定义为ReDim myArray(0,i) Then look at how you're calling it later, myArray(i) = foundRow.Row . 然后查看您稍后如何调用它, myArray(i) = foundRow.Row

The second index is missing. 第二个索引丢失。 Try doing myArray(0,i) = foundRow.Row . 尝试做myArray(0,i) = foundRow.Row

Then, to return all the foundrows, you can do something like 然后,要返回所有发现的行,您可以执行以下操作

for i = lbound(myArray) to ubound(myArray)
 debug.print "Rows are: " & myArray(0,i)
next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM