Arrays Excel VBA:是否可以获取循环中每个单元格的地址并在循环外使用它们

Arrays Excel VBA:是否可以获取循环中每个单元格的地址并在循环外使用它们,arrays,vba,excel,loops,Arrays,Vba,Excel,Loops,我想在工作簿的一些工作表中写一个循环,以获得每个工作表中一个特定单元格的地址。我的目的是获取这些单元格的地址,并将它们用作循环外的引用 我写了一个代码,但它不能像我想要的那样工作: Sub RegionalAverage() For i = 1 To 2 Sheets(i).Activate Range("A1").Select Selection.AutoFilter ActiveSheet.Range("A1:H23393").AutoFilter

我想在工作簿的一些工作表中写一个循环,以获得每个工作表中一个特定单元格的地址。我的目的是获取这些单元格的地址,并将它们用作循环外的引用

我写了一个代码,但它不能像我想要的那样工作:

Sub RegionalAverage()

    For i = 1 To 2
    Sheets(i).Activate
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("A1:H23393").AutoFilter Field:=6, Criteria1:=1
    Columns("A:H").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Find(What:="1/1/2008", After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Select
    ActiveCell.Offset(0, 4).Select
    Name (i) = "'" & Selection.Parent.name & "'" & "!" & Selection.Address(External:=False)

    Next i

    MsgBox Name(1)
    MsgBox Name(2)

End Sub

我已经重写了您的过程,以避免使用。选择“”并使用Explicit²ENVIEW选项强制声明所使用的变量。我怀疑manby的问题源于使用未声明和未标注尺寸的
Name
数组

Option Explicit

Sub RegionalAverage()
    'declare the variables you plan to use!!!!!
    Dim i As Long, fnd As Range, aNames As Variant

    'you were only looking for two addresses so dimension the array now
    ReDim aNames(1 To 2)

    'loop through the first two worksheets
    For i = 1 To 2
        'start isolating the workspace ujsing With ... End With
        With Worksheets(i)
            'if AutoFilter is active, turn it off
            If .AutoFilterMode Then .AutoFilterMode = False
            ''work with the 'island' of data radiating out from A1
            With .Cells(1, "A").CurrentRegion
                'isolate to A:H
                With .Resize(.Rows.Count, 8)
                    'filter on column F = 1
                    .AutoFilter Field:=6, Criteria1:=1
                    'isolate to the visible cells
                    With .SpecialCells(xlCellTypeVisible)
                        'set a range object to the first found cell
                        Set fnd = .Cells.Find(What:="1/1/2008", After:=.Cells(.Cells.Count), _
                                              LookIn:=xlFormulas, LookAt:=xlPart, _
                                              SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                              MatchCase:=False, SearchFormat:=False)
                        'check if anything was found
                        If Not fnd Is Nothing Then
                            'offset 4 columns to the right
                            Set fnd = fnd.Offset(0, 4)
                            'store the parent worksheet name and cell address
                            aNames(i) = Chr(39) & fnd.Parent.Name & "'!" & fnd.Address(External:=False)
                        End If
                    End With
                End With
            End With
        End With
    Next i

    MsgBox "First found at " & aNames(1) & vbLf & _
           "Second found at " & aNames(2)

End Sub
请注意,我的数组名为
aNames
。在VBA中被视为“保留字”,将保留字、方法或属性重新用作变量不被视为“最佳实践”


.1请参阅,以了解更多摆脱依靠选择和激活来实现目标的方法

²设置需要在VBE的工具中声明变量► 选择权► 编辑器属性页将把语句放在每个新创建的代码表的顶部。这 将避免愚蠢的编码错误,如拼写错误,以及影响您在变量中使用正确的变量类型 宣言。在没有声明的情况下动态创建的变量都是变量/对象类型。使用选项显式是 被广泛认为是“最佳实践”