第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用VBA循環(huán)遍歷文件夾中的文件?

使用VBA循環(huán)遍歷文件夾中的文件?

瀟湘沐 2019-05-30 10:15:22
使用VBA循環(huán)遍歷文件夾中的文件?我想使用以下方法循環(huán)遍歷目錄的文件VBA在Excel 2010中。在循環(huán)中,我需要文件名格式化文件的日期。我已經(jīng)編寫了以下代碼,如果文件夾中沒有超過50個文件,它可以正常工作,否則速度會慢得可笑(我需要它處理超過10000個文件的文件夾)。此代碼的唯一問題是要查找的操作file.name需要非常長的時間。工作但速度太慢的代碼(每100個文件15秒):Sub LoopThroughFiles()    Dim MyObj As Object, MySource As Object, file As Variant    Set MySource = MyObj.GetFolder("c:\testfolder\")    For Each file In MySource.Files      If InStr(file.name, "test") > 0 Then          MsgBox "found"          Exit Sub       End If    Next fileEnd Sub解決問題:下面的解決方案已經(jīng)解決了我的問題Dir以一種特定的方式(對于15000個文件使用20秒)和使用命令檢查時間戳FileDateTime.考慮到另一個答案,從下面的20秒減少到不到1秒。
查看完整描述

4 回答

?
天涯盡頭無女友

TA貢獻1831條經(jīng)驗 獲得超9個贊

Dir使用外卡,這樣您就可以在添加過濾器的情況下做出很大的改變。test預先準備并避免對每個文件進行測試

Sub LoopThroughFiles()
    Dim StrFile As String
    StrFile = Dir("c:\testfolder\*test*")
    Do While Len(StrFile) > 0
        Debug.Print StrFile
        StrFile = Dir    LoopEnd Sub


查看完整回答
反對 回復 2019-05-30
?
湖上湖

TA貢獻2003條經(jīng)驗 獲得超2個贊

迪爾看起來非???。

Sub LoopThroughFiles()
    Dim MyObj As Object, MySource As Object, file As Variant
   file = Dir("c:\testfolder\")
   While (file <> "")
      If InStr(file, "test") > 0 Then
         MsgBox "found " & file         Exit Sub
      End If
     file = Dir  WendEnd Sub


查看完整回答
反對 回復 2019-05-30
?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

Dir函數(shù)是可行的,但是問題是您不能使用Dir函數(shù)遞歸,如所述在這里,朝向底部.

我處理這個問題的方法是使用Dir函數(shù)獲取目標文件夾的所有子文件夾,并將它們加載到數(shù)組中,然后將數(shù)組傳遞給遞歸的函數(shù)。

這是我寫的一個類,它包括搜索過濾器的能力。(你必須原諒匈牙利的符號,這是在它風靡一時的時候?qū)懙摹?/em>)

Private m_asFilters() As StringPrivate m_asFiles As VariantPrivate m_lNext As LongPrivate m_lMax As LongPublic Function 
GetFileList(ByVal ParentDir As String, Optional ByVal sSearch As String, Optional ByVal Deep As Boolean = True) As Variant
    m_lNext = 0
    m_lMax = 0

    ReDim m_asFiles(0)
    If Len(sSearch) Then
        m_asFilters() = Split(sSearch, "|")
    Else
        ReDim m_asFilters(0)
    End If

    If Deep Then
        Call RecursiveAddFiles(ParentDir)
    Else
        Call AddFiles(ParentDir)
    End If

    If m_lNext Then
        ReDim Preserve m_asFiles(m_lNext - 1)
        GetFileList = m_asFiles    End IfEnd FunctionPrivate Sub RecursiveAddFiles(ByVal ParentDir As String)
    Dim asDirs() As String
    Dim l As Long
    On Error GoTo ErrRecursiveAddFiles    'Add the files in 'this' directory!


    Call AddFiles(ParentDir)

    ReDim asDirs(-1 To -1)
    asDirs = GetDirList(ParentDir)
    For l = 0 To UBound(asDirs)
        Call RecursiveAddFiles(asDirs(l))
    Next l    On Error GoTo 0Exit SubErrRecursiveAddFiles:End SubPrivate Function GetDirList(ByVal ParentDir As String) As String()
    Dim sDir As String
    Dim asRet() As String
    Dim l As Long
    Dim lMax As Long

    If Right(ParentDir, 1) <> "\" Then
        ParentDir = ParentDir & "\"
    End If
    sDir = Dir(ParentDir, vbDirectory Or vbHidden Or vbSystem)
    Do While Len(sDir)
        If GetAttr(ParentDir & sDir) And vbDirectory Then
            If Not (sDir = "." Or sDir = "..") Then
                If l >= lMax Then
                    lMax = lMax + 10
                    ReDim Preserve asRet(lMax)
                End If
                asRet(l) = ParentDir & sDir
                l = l + 1
            End If
        End If
        sDir = Dir    Loop
    If l Then
        ReDim Preserve asRet(l - 1)
        GetDirList = asRet()
    End IfEnd FunctionPrivate Sub AddFiles(ByVal ParentDir As String)
    Dim sFile As String
    Dim l As Long

    If Right(ParentDir, 1) <> "\" Then
        ParentDir = ParentDir & "\"
    End If

    For l = 0 To UBound(m_asFilters)
        sFile = Dir(ParentDir & "\" & m_asFilters(l), vbArchive Or vbHidden Or vbNormal Or vbReadOnly Or vbSystem)
        Do While Len(sFile)
            If Not (sFile = "." Or sFile = "..") Then
                If m_lNext >= m_lMax Then
                    m_lMax = m_lMax + 100
                    ReDim Preserve m_asFiles(m_lMax)
                End If
                m_asFiles(m_lNext) = ParentDir & sFile
                m_lNext = m_lNext + 1
            End If
            sFile = Dir        Loop
    Next lEnd Sub


查看完整回答
反對 回復 2019-05-30
  • 4 回答
  • 0 關注
  • 3726 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號