【问题标题】:Loop a function that runs on files in a folder循环在文件夹中的文件上运行的函数
【发布时间】:2019-12-13 22:23:39
【问题描述】:

我有一个宏要在我在互联网上找到的宏中使用。

第二个宏遍历文件夹内的所有 Excel 文件:

Sub RunOnAllFilesInFolder()
    Dim folderName As String, eApp As Excel.Application, fileName As String
    Dim wb As Workbook, ws As Worksheet, currWs As Worksheet, currWb As Workbook
    Dim fDialog As Object: Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    Set currWb = ActiveWorkbook: Set currWs = ActiveSheet

    'Select folder in which all files are stored
    fDialog.Title = "Select a folder"
    fDialog.InitialFileName = currWb.Path
    If fDialog.Show = -1 Then
        folderName = fDialog.SelectedItems(1)
    End If

    'Create a separate Excel process that is invisibile
    Set eApp = New Excel.Application:  eApp.Visible = False

    'Search for all files in folder [replace *.* with your pattern e.g. *.xlsx]
    fileName = Dir(folderName & "\*.*")
    Do While fileName <> ""
        'Update status bar to indicate progress
        Application.StatusBar = "Processing " & folderName & "\" & fileName

        Set wb = eApp.Workbooks.Open(folderName & "\" & fileName)
        '...
        'YOUR CODE HERE
        '...
        wb.Close SaveChanges:=False 'Close opened worbook w/o saving, change as needed
        Debug.Print "Processed " & folderName & "\" & fileName 
        fileName = Dir()
    Loop
    eApp.Quit
    Set eApp = Nothing
    'Clear statusbar and notify of macro completion
    Application.StatusBar = ""
    MsgBox "Completed executing macro on all workbooks"
End Sub

我制作了一个宏,它基于文件中的三个命名单元格来查找范围并更改其他一些范围的样式。

并非所有 Excel 文件都具有所有三个命名单元格,因此当范围无效时我需要代码工作。

我尝试使用错误处理程序,但收到以下错误:

“循环不做”

当范围不存在并且还发现错误时,我尝试了 IF 和 else。

我的代码:

Dim test As Worksheet
Dim rOutstandingR As Range
Dim rAdditionalDueR As Range
Dim rFollowingR As Range
Dim rOutstandingBorderR As Range
Dim rAdditionalDueBorderR As Range
Dim rFollowingBorderR As Range
Dim ORow As Long
Dim OCol As Long
Dim ARow As Long
Dim ACol As Long
Dim FRow As Long
Dim FCol As Long
Dim OutstandingTopBorderRange As Range
Dim OutstandingBottomBorderRange As Range
Dim OutstandingRightBorderRange As Range
Dim AdditionalDueTopBorderRange As Range
Dim AdditionalDueBottomRange As Range
Dim AdditinalDueRightBorderRange As Range
Dim FollowingTopBorderRange As Range
Dim FollowingBottomBorderRange As Range
Dim FollowingRightBorderRange As Range
Dim OutstandingTextRange As Range
Dim AdditionalDueTextRange As Range
Dim FollowingTextRange

With Range("A1:Z999")

    'Setting up another range that may not exists within excel file and give an error 

    Set rOutstandingR = ActiveSheet.Range("Outstanding")
    rOutstandingBorderR = rOutstandingR.Address
    rOutstandingR.Select

‘more code in which I change format of cells based on range

    'Setting up another range that may not exists within excel file and give an error  

    Set rAdditionalDueR = ActiveSheet.Range("AdditionalDue")
    rAdditionalDueBorderR = rAdditionalDueR.Address
    rAdditionalDueR.Select

‘more code in which I change format of cells based on range

'Setting up another range that may not exists within excel file and give an error    
    'Setting Up rFollowingR  as Range for Following Variable
    Set rFollowingR = ActiveSheet.Range("Following")
    rFollowingBorderR = rFollowingR.Address
    rFollowingR.Select

‘more code in which I change format of cells based on range

您可以通过范围的数量来想象,中间有很多代码,但它仅基于命名单元格“杰出”、“附加到期”和“以下”的三个主要范围。

我需要范围之间的所有代码都能正常工作,如果第一个范围不存在,则进行验证,然后进行格式更改等。

我尝试放置一些错误处理程序(恢复标签),但由于循环遍历所有文件,当我在第一个宏中使用上述代码时无法修复它。

如何放置错误处理程序,以便可以在运行在文件文件夹上的宏中使用此宏。

【问题讨论】:

    标签: excel vba loops range


    【解决方案1】:

    有两种方法可以处理这个问题,但是如果使用 sn-ps 来测试您正在处理的内容并不简单。您可能需要考虑将代码分成多个子/函数。

    假设您希望运行某种类型的处理代码,该解决方案应该是干净的:

    With range("A1:Z999")
    
        'Setting up another range that may not exists within excel file and give an error
        On Error GoTo OutstandingError
    
        Set rOutstandingR = ActiveSheet.range("Outstanding")
        rOutstandingBorderR = rOutstandingR.Address
        rOutstandingR.Select
    
    OutstandingResume:
    
        'more code in which I change format of cells based on range
    
        'Setting up another range that may not exists within excel file and give an error
        On Error GoTo AdditionalDueError
    
        Set rAdditionalDueR = ActiveSheet.range("AdditionalDue")
        rAdditionalDueBorderR = rAdditionalDueR.Address
        rAdditionalDueR.Select
    
    AdditionalDueResume:
    
        'more code in which I change format of cells based on range
    
        'Setting up another range that may not exists within excel file and give an error
        'Setting Up rFollowingR  as Range for Following Variable
        On Error GoTo FollowingError
    
        Set rFollowingR = ActiveSheet.range("Following")
        rFollowingBorderR = rFollowingR.Address
        rFollowingR.Select
    
    FollowingResume:
    
        'more code in which I change format of cells based on range
    
        GoTo Complete
    OutstandingError:
        'Error handling code here
        Resume OutstandingResume
    
    AdditionalDueError:
        'Error handling code here
        Resume AdditionalDueResume
    
    FollowingError:
        'Error handling code here
        Resume FollowingResume
    
    Complete:
    

    此解决方案只是完全绕过该块,无需任何处理代码:

    With range("A1:Z999")
    
        'Setting up another range that may not exists within excel file and give an error
        On Error GoTo OutstandingResume
    
        Set rOutstandingR = ActiveSheet.range("Outstanding")
        rOutstandingBorderR = rOutstandingR.Address
        rOutstandingR.Select
    
    OutstandingResume:
    
        'more code in which I change format of cells based on range
    
        'Setting up another range that may not exists within excel file and give an error
        On Error GoTo AdditionalDueResume
    
        Set rAdditionalDueR = ActiveSheet.range("AdditionalDue")
        rAdditionalDueBorderR = rAdditionalDueR.Address
        rAdditionalDueR.Select
    
    AdditionalDueResume:
    
        'more code in which I change format of cells based on range
    
        'Setting up another range that may not exists within excel file and give an error
        'Setting Up rFollowingR  as Range for Following Variable
        On Error GoTo FollowingResume
    
        Set rFollowingR = ActiveSheet.range("Following")
        rFollowingBorderR = rFollowingR.Address
        rFollowingR.Select
    
    FollowingResume:
    
        'more code in which I change format of cells based on range
    

    如果您想换个方向,这里有一个函数,它返回一个布尔值来判断命名范围是否存在。使用它,您可以重构它以使用条件而不是依赖错误检查和换行。

    Private Function BET_RangeNameExists(nname) As Boolean 
    Dim n As Name 
        BET_RangeNameExists = False 
        For Each n In ActiveWorkbook.Names 
            If UCase(n.Name) = UCase(nname) Then 
                BET_RangeNameExists = True 
                Exit Function 
            End If 
        Next n 
    End Function
    

    取自https://bettersolutions.com/excel/named-ranges/vba-named-range-exists.htm

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      相关资源
      最近更新 更多