【问题标题】:Get Current Position in Excel Vba Loop在 Excel Vba 循环中获取当前位置
【发布时间】:2022-11-30 04:53:15
【问题描述】:

我有一个当前查找包含单词“有效日期”的单元格,它基本上是我正在循环的列表的列标题(恰好从单元格 E24 开始)。我想抓住我所在的当前位置来开始我的循环,而不是硬编码 i = 24 到 200(所以 24 应该是当前单元格)。我该怎么做呢?下面是我的循环

' Find beginning of list of specification files
Range("A1").Select
    Cells.Find(What:="Effective Date", After:=ActiveCell, LookIn:=xlFormulas2 _
        , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset(2, 0).Select
        
 
 Dim i
 Dim Folder

    For i = 24 To 200
      If IsEmpty(Range("E" & i).Value) Or IsEmpty(Range("G" & i).Value) Then
      ' do nothing
      Else
            Range("G" & i).Select
            Folder = ActiveCell
        Call ListFilesInFolder(Folder, DestinationLoc)
        End If
    Next i
    

    
End Sub

【问题讨论】:

    标签: excel vba file copy


    【解决方案1】:

    尚不完全清楚您要做什么,我已经更新了代码以提高效率并添加了一些 cmets/questions。

    'You should define what your variable types are
    Dim oFound As Object
    Dim i As Long
    Dim Folder As Range
    Dim DestinationLoc 'as ????
    
    'Find beginning of list of specification files
    'Range("A1").Select  'Select as few things as possible - that takes time
    
    '
    Set oFound = ActiveSheet.UsedRange.Cells.Find(What:="Effective Date", LookIn:=xlFormulas2, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)  'Probably should use xlWhole instaaed of xlPart
    If Not oFound Is Nothing Then
        'Found a cell containing the searched for value
        'ofound is a range that can be accessed to get any info about that range
        'ActiveCell.Offset(2, 0).Select   why select this cell??
        'oFound.Row + 1 is the row below "Effective Date"
        
        
        For i = oFound.Row + 1 To 200   'Where does 200 come from?
            If IsEmpty(Range("E" & i).Value) Or IsEmpty(Range("G" & i).Value) Then
                ' do nothing
            Else
                'Range("G" & i).Select
                'Folder = ActiveCell
                'Replace the above 2 lines iwth
                Folder = Range("G" & i).Value
                
                Call ListFilesInFolder(Folder, DestinationLoc)   'where does DestinationLoc value come from?
            End If
        Next i
    
    Else
            MsgBox "'Effective Date' not found"
    
    End If
    

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 2016-11-12
      • 2020-11-23
      • 2013-09-06
      • 2023-01-23
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      相关资源
      最近更新 更多