+ Reply to Thread
Results 1 to 4 of 4

Macro code to continue iterating if error.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-18-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    5

    Macro code to continue iterating if error.

    Hi,

    I am trying to rename 2 tabs each on 60 files, this is the code I have below.

    Sub DirLoop2()
        Application.ScreenUpdating = False
        Application.DisplayAlerts = 0
          
          Dim MyFile As String, Sep As String
          
          
          Sep = Application.PathSeparator
    
          If Sep = "\" Then
          
             MyFile = Dir("C:\Sales Forecasting test bed\Customers" & Sep & "*.xls")
    
          Do While MyFile <> ""
          
        A = "C:\Sales Forecasting test bed\Customers\" & MyFile
            
        Workbooks.Open (A)
        Sheets("EXCEPTION 3 MONTH").Select
        Sheets("EXCEPTION 3 MONTH").Name = "EXCEPTIONAL 3 MONTH"
        Sheets("EXCEPTION 5 MONTH").Select
        Sheets("EXCEPTION 5 MONTH").Name = "EXCEPTIONAL 5 MONTH"
        
        'Finished
        
        ActiveWorkbook.Save
        ActiveWindow.Close
        
        
           'End of main macro stuff
        
             MyFile = Dir()
          Loop
     Application.ScreenUpdating = True
       End Sub
    The problem is, some of the files have the tabs named correctly or have a name other than the one specified in my macro.

    Eg: 70% of the files have the tabs named "EXCEPTION 3 MONTH", 20% have "EXCPTION 3 MONTH" and 10% have "EXCEPTIONAL 3 MONTH" as the tab names.

    Is there a way I could I force all other names of the tabs to be changed to the new one ?

    Thanks,
    Last edited by sid9221; 03-23-2013 at 03:15 PM.

  2. #2
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Macro code to continue iterating if error.

    try using this in your code

    Sub checksheets()
    On Error Resume Next
    Worksheets("EXCEPTION 3 MONTH").Name = "EXCEPTIONAL 3 MONTH"
    If Err <> 0 Then
        Worksheets("EXCPTION 3 MONTH").Name = "EXCEPTIONAL 3 MONTH"
        If Err <> 0 Then
            Worksheets("EXCEPTIONAL 3 MONTH").Name = "EXCEPTIONAL 3 MONTH"
            If Err <> 0 Then
            'do nothing
            Else
            MsgBox "Sheet1 Present"
            End If
        Else
        MsgBox "Help Sheet Present"
        End If
    Else
        MsgBox "Help Sheet Present"
    End If
    On Error GoTo 0
    End Sub
    Regards
    Sean

    Please add to my reputation if you think i helped
    (click on the star below the post)
    Mark threads as "Solved" if you have your answer
    (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code:
    [code] Your code here [code]
    Please supply a workbook containing example Data:
    It makes its easier to answer your problem & saves time!

  3. #3
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Macro code to continue iterating if error.

    sorry, forgot to take the msgbox's out of the code.
    Just using those to check it was working.

    Sub checksheets()
    On Error Resume Next
    Worksheets("EXCEPTION 3 MONTH").Name = "EXCEPTIONAL 3 MONTH"
    If Err <> 0 Then
        Worksheets("EXCPTION 3 MONTH").Name = "EXCEPTIONAL 3 MONTH"
        If Err <> 0 Then
            Worksheets("EXCEPTIONAL 3 MONTH").Name = "EXCEPTIONAL 3 MONTH"
            If Err <> 0 Then
            'do nothing
            End If
        End If
    End If
    On Error GoTo 0
    End Sub

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Macro code to continue iterating if error.

    I haven't tested this but something like this is what you need.


    Sub DirLoop2()
        Application.ScreenUpdating = False
        Application.DisplayAlerts = 0
          
          Dim MyFile As String, Sep As String
          
          
          Sep = Application.PathSeparator
    
          If Sep = "\" Then
          
             MyFile = Dir("C:\Sales Forecasting test bed\Customers" & Sep & "*.xls")
    
          Do While MyFile <> ""
          
        A = "C:\Sales Forecasting test bed\Customers\" & MyFile
            
        Workbooks.Open (A)
    on error goto 10
        Sheets("EXCEPTION 3 MONTH").Select: goto 100
    
    10     on error goto 20
    Sheets("EXCPTION 3 MONTH").Select: goto 100
    
    20     Sheets("EXCEPTIONAL 3 MONTH").Select
    
    100 activesheet.Name = "EXCEPTIONAL 3 MONTH"
    
    on error goto 110
        Sheets("EXCEPTION 5 MONTH").Select: goto 200
    
    110     on error goto 120
    Sheets("EXCPTION 5 MONTH").Select: goto 200
    
    120     Sheets("EXCEPTIONAL 5 MONTH").Select
    
    200 activesheet.Name = "EXCEPTIONAL 5 MONTH"
        
        'Finished
        
        ActiveWorkbook.Save
        ActiveWindow.Close
        
        
           'End of main macro stuff
        
             MyFile = Dir()
          Loop
     Application.ScreenUpdating = True
       End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1