【问题标题】:Excel VBA : Is it possible to get address of each cell in a loop and use them out of loopExcel VBA:是否可以在循环中获取每个单元格的地址并在循环外使用它们
【发布时间】:2016-11-12 14:32:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: arrays vba excel loops


    【解决方案1】:

    我已经重写了您的过程以避免使用 .Select¹ 并使用 Option Explicit² 环境来强制声明所使用的变量。我怀疑你的问题源于使用未声明和未标注的 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 中被视为“保留字”,将保留字、方法或属性重新用作变量不被视为“最佳实践”。


    请参阅How to avoid using Select in Excel VBA macros,了解更多摆脱依赖选择和激活来实现目标的方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2018-05-10
      • 2019-03-31
      相关资源
      最近更新 更多