+ Reply to Thread
Results 1 to 9 of 9

Error Handling in loop - go to next x

Hybrid View

Phiabell Error Handling in loop - go... 02-04-2013, 08:27 AM
Norie Re: Error Handling in loop -... 02-04-2013, 08:40 AM
Andrew-R Re: Error Handling in loop -... 02-04-2013, 08:49 AM
Phiabell Re: Error Handling in loop -... 02-04-2013, 08:53 AM
Andrew-R Re: Error Handling in loop -... 02-04-2013, 09:01 AM
Norie Re: Error Handling in loop -... 02-04-2013, 09:12 AM
Phiabell Re: Error Handling in loop -... 02-04-2013, 09:24 AM
Norie Re: Error Handling in loop -... 02-04-2013, 09:27 AM
Phiabell Re: Error Handling in loop -... 02-04-2013, 09:30 AM
  1. #1
    Registered User
    Join Date
    11-30-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    20

    Error Handling in loop - go to next x

    Hi,

    I am trying to handle errors in my macro. The code goes down a number of rows, opens a worksheet based on that row, and copys a value from that worksheet into a cell in that row, then moves down.

    On a error I'd like to

    a) have the word 'error' in the cell where the value would be and
    b) skip to the next row

    this is the code I have so far - but this does not skip to the next x, but resumes in the middle of the code which has the effect of trying to shut down my main workbook (as the code says close workbook, at which point the secondary sheet would be open, but isn't, as the error is generally in finding the file to open.)

    How can I get it to skip to next x?

    Sub RV10()
    '
    ' RV10 Macro
    '
            
            Dim x As Long
            For x = 6 To 37
            On Error GoTo ErrorHandler
            Workbooks.Open Range("S" & x).Value
            Range("D191").Select
            Selection.Copy
            Windows("LB Billings Figures 2013.xls").Activate
            Range("D" & x).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Workbooks(Sheets("Details").Range("T" & x).Value).Activate
            ActiveWindow.Close
            Calculate
            Next x
            
    ErrorHandler:
    
            Range("D" & x).Select
            ActiveCell.FormulaR1C1 = "Error"
            Resume Next
            Exit Sub
            
            End Sub
    Thanks for help

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Error Handling in loop - go to next x

    What's the cause of the error?

    Which line of code does it happen on?

    Is the problem that some of the workbooks you are trying to open don't exist?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Error Handling in loop - go to next x

    The straight path through the mud would be:

    Sub RV10()
    '
    ' RV10 Macro
    '
            
            Dim x As Long
            For x = 6 To 37
            On Error GoTo ErrorHandler
            Workbooks.Open Range("S" & x).Value
            Range("D191").Select
            Selection.Copy
            Windows("LB Billings Figures 2013.xls").Activate
            Range("D" & x).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Workbooks(Sheets("Details").Range("T" & x).Value).Activate
            ActiveWindow.Close
            Calculate
    ResumePoint:
            Next x
            
    ErrorHandler:
    
            Range("D" & x).Select
            ActiveCell.FormulaR1C1 = "Error"
            Resume ResumePoint
            Exit Sub
            
            End Sub
    But it would be far better to test for the existence of each file before you tried to open it:

    If Dir(Range("S" & x).Value) <> "" Then

  4. #4
    Registered User
    Join Date
    11-30-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Error Handling in loop - go to next x

    Andrew, thank you

    That's what I was looking for - I had the components before I think but they weren't in the right order.
    I think this is the solution I want as often the error will be the file path having changed name slightly - so the value will exist but the file won't. The 'error' return will prompt user to update the value.

    Only problem is, having tested this new code, is that although x = 6 to 37, it keeps going and entering 'error' thousands of lines down until excel crashed :-/

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Error Handling in loop - go to next x

    You need to move your Exit Sub Line from where it is to the line immediately above where the error handler starts...

    Sub RV10()
    '
    ' RV10 Macro
    '
            
            Dim x As Long
            For x = 6 To 37
            On Error GoTo ErrorHandler
            Workbooks.Open Range("S" & x).Value
            Range("D191").Select
            Selection.Copy
            Windows("LB Billings Figures 2013.xls").Activate
            Range("D" & x).Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            Workbooks(Sheets("Details").Range("T" & x).Value).Activate
            ActiveWindow.Close
            Calculate
    ResumePoint:
            Next x
            
           Exit Sub
    
    ErrorHandler:
    
            Range("D" & x).Select
            ActiveCell.FormulaR1C1 = "Error"
            Resume ResumePoint
            
            End Sub

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Error Handling in loop - go to next x

    This is what I as thinking of.
    Option Explicit
    
    Sub RV10()
    '
    ' RV10 Macro
    '
    Dim ws As Worksheet
    Dim wbOpen As Workbook
    Dim x As Long
        
        Set ws = ActiveSheet    ' change active sheet to the worksheet with filenames
    
        For x = 6 To 37
            If Len(Dir(ws.Range("S" & x).Value)) > 0 Then
            
                Set wbOpen = Workbooks.Open(ws.Range("S" & x).Value)
    
                wbOpen.ActiveSheet.Copy
    
                ws.Range("D" & x).PasteSpecial xlPasteValues
    
                wbOpen.Close SaveChanges:=False
                
                Calculate
            Else
                ws.Range("D" & x).Value = "Error"
            End If
        Next x
    
    End Sub

  7. #7
    Registered User
    Join Date
    11-30-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Error Handling in loop - go to next x

    Thank you, works perfectly.

    Norie thanks for the alternative option, though I think I will go with the other due to the value sometimes being present but incorrect.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Error Handling in loop - go to next x

    The code I posted doesn't check to see if a value is there.

    It's checking for the file and path the value is supposed to be referring to.

    If they aren't found 'Error' is put in column D.

  9. #9
    Registered User
    Join Date
    11-30-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Error Handling in loop - go to next x

    Ah ok, thanks, I will try that too and see if one suits the process better.

+ 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