Excel VBA:是否可以在循环中获取每个单元格的地址并将其用于循环

时间:2016-07-10 15:27:41

标签: arrays excel vba excel-vba loops

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

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

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

1 个答案:

答案 0 :(得分:2)

我已经重写了你的程序以避免使用。选择¹并使用OptionExplicit²环境来强制声明所使用的变量。我怀疑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

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

有关远离依赖select和activate以实现目标的更多方法,请参阅How to avoid using Select in Excel VBA macros

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