Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA将找到的单元格地址添加到数组_Excel_Vba - Fatal编程技术网

Excel VBA将找到的单元格地址添加到数组

Excel VBA将找到的单元格地址添加到数组,excel,vba,Excel,Vba,我有一些代码取自cpearson.com“查找所有”页面,让我查找给定范围内所有出现的搜索值 我希望做的是将找到的单元格的.address传递给一个数组,以便稍后在代码中使用 Dim desk As String Dim rng As Range Dim itm() As Variant Dim lastc As Range Dim found As Variant Dim firstaddr As String Set rng = Sheets("Inventory").Range("A1:

我有一些代码取自cpearson.com“查找所有”页面,让我查找给定范围内所有出现的搜索值

我希望做的是将找到的单元格的.address传递给一个数组,以便稍后在代码中使用

Dim desk As String
Dim rng As Range
Dim itm() As Variant
Dim lastc As Range
Dim found As Variant
Dim firstaddr As String

Set rng = Sheets("Inventory").Range("A1:A200")

desk = ActiveSheet.Shapes(Application.Caller).TextFrame.Characters.Text

countd = Application.WorksheetFunction.CountIf(rng, desk)
'MsgBox desk
'MsgBox countd

With rng
    Set lastc = .Cells(.Cells.count)
End With

Set found = rng.find(desk, lastc, , xlWhole)

If Not found Is Nothing Then
    firstaddr = found.Address
End If

Do Until found Is Nothing
    ReDim Preserve itm(found.Address)  'get error 13 type mismatch here 
    itm(found.Address) = found.Address + 1
    'Debug.Print found.Address
    Set found = rng.FindNext(found)

    If found.Address = firstaddr Then
        Exit Do
    End If
Loop

End Sub
更新:

Dim desk As String
Dim countd As Long    
Dim rng As Range
Dim itm()
Dim lastc As Range
Dim found As Range
Dim firstaddr As String
Dim i As Integer

Set rng = Sheets("Inventory").Range("A1:A200")

desk = ActiveSheet.Shapes(Application.Caller).TextFrame.Characters.Text

countd = Application.WorksheetFunction.CountIf(rng, desk)
'MsgBox desk
'MsgBox countd
ReDim itm(countd)
With rng
    Set lastc = .Cells(.Cells.count)
End With

Set found = rng.find(desk, lastc, , xlWhole)

If Not found Is Nothing Then
    firstaddr = found.Address
End If
i = 0
Do Until found Is Nothing
    itm(i) = found.Address

    'Debug.Print found.Address
    Set found = rng.FindNext(found)

    If found.Address = firstaddr Then
        Exit Do
    End If
    i = i + 1
Loop

MsgBox "array is " & Join(itm, ", ")

End Sub
经过更多的谷歌搜索,在下面2个答案的帮助下,我能够在不改变代码的情况下获得所需的输出


现在,要使用该数组的每个元素搜索右侧单元格中的特定值,然后根据其响应获取正在搜索的单元格右侧的单元格值。但这可能是另一个问题

CPearson的
FindAll
函数返回一个
Range
对象。要获取包含地址的数组,请声明该数组并按
TestFindAll
中的操作循环数组内容:

Function GetAddresses(rng as Range) As String()

    Dim addresses() As String
    Redim addresses(rng.Count - 1)

    Dim i As Integer
    i = 0

    Dim cell As Range
    For Each cell In rng

        addresses(i) = cell.Address
        i = i + 1

    Next cell

    GetAddresses = addresses

End Function 

下面是一种在工作表中查找所有“幸福”事件并将单元格地址保存在动态数组中的方法。它使用Find()FindNext():

Sub dural()
    Dim s As String, r As Range, ary() As String
    ReDim ary(1)
    s = "happiness"
    Set r = Cells.Find(What:=s, After:=Range("A1"))
    ary(UBound(ary)) = r.Address(0, 0)

    Do
        Set r = Cells.FindNext(After:=r)
        If r Is Nothing Then Exit Do
        If ary(1) = r.Address(0, 0) Then Exit Do
        ReDim Preserve ary(UBound(ary) + 1)
        ary(UBound(ary)) = r.Address(0, 0)
    Loop

    MsgBox UBound(ary)
End Sub

你能提供代码或代码本身的链接吗?还有一个你想要的模型?WWW.cpearson.com/excel/findall.aspx就在我得到代码的页面上的find all函数之前。红线上方。您希望TestFindAll将地址打印到数组而不是控制台吗?我看不出您在代码中的何处使用
FindAll
函数。另外,
itm(found.Address)
ReDim Preserve itm(found.Address)
未定义,因为
found.Address
是字符串,而不是整数或长字符串。