Error Handling in a Loop

RLJ

Active Member
Joined
Mar 15, 2011
Messages
417
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a workbook that is a main tracking workbook that needs to look up data from a user selected workbook. What I need it to do is to loop through column B in the Main tracking workbook, look up the value from column b in the user selected workbook and copy/paste data over to the main tracking workbook. This works fine if it finds all the values in the user selected workbook, however the values from the main tracing workbook come from multiple user selected workbooks so there will be errors. I need help in looping through the errors. When it gets to the second error the code stops. I need to have each error cleared as it runs through the looping code.

Code:
Sub FindKey()


    '/////////////////////////////////////////////////////////////////////////
    
    
    '/////////////////////////////////////////////////////////////////////////
    
    Application.ScreenUpdating = False
    Dim lookVal As String
NextProp:
    Windows(mdt).Activate
    
    Sheets("Deal Tracker").Select
    ActiveCell.Offset(1, 0).Select 'Looks up Porperty Name
    
    Do Until IsEmpty(ActiveCell.Value)
        
    lookVal = ActiveCell.Value 'Property Name
     
    Windows(ws).Activate
    Sheets("Tracker").Select 'Main Sheet that is being Updated
    
    On Error GoTo NextProp
    Cells.Find(What:=lookVal, After:=ActiveCell, LookIn:= _
        xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
        Range("C" & (ActiveCell.Row)).Select
        
    ActiveCell.Offset(0, 9).Copy
    
    Windows(mdt).Activate
    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False
        
    Windows(ws).Activate
    Sheets("Tracker").Select 'Main Sheet that is being Updated
    ActiveCell.Offset(0, 10).Copy


    Windows(mdt).Activate
    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False
        
    Windows(ws).Activate
    Sheets("Tracker").Select 'Main Sheet that is being Updated
    ActiveCell.Offset(0, 19).Copy


    Windows(mdt).Activate
    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False
        
    Windows(ws).Activate
    Sheets("Tracker").Select 'Main Sheet that is being Updated
    ActiveCell.Offset(0, 20).Copy


    Windows(mdt).Activate
    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False


    ActiveCell.Offset(1, -5).Select
    Loop
    
    Application.ScreenUpdating = True
    
End Sub

Thanks for your help!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
RLJ,

You might consider moving the NextProp: label inside the Loop and adding an If Err.Number statement...

Code:
Sub FindKey()
    Application.ScreenUpdating = False
    Dim lookVal As String

    Windows(mdt).Activate
    Sheets("Deal Tracker").Select
    ActiveCell.Offset(1, 0).Select 'Looks up Porperty Name
    
    On Error GoTo NextProp
    Do Until IsEmpty(ActiveCell.Value)
        lookVal = ActiveCell.Value 'Property Name
        Windows(ws).Activate
        Sheets("Tracker").Select 'Main Sheet that is being Updated
        Cells.Find(What:=lookVal, After:=ActiveCell, LookIn:= _
            xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        Range("C" & (ActiveCell.Row)).Select
        ActiveCell.Offset(0, 9).Copy
        
        Windows(mdt).Activate
        Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
        ActiveCell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
            :=False, Transpose:=False
            
        Windows(ws).Activate
        Sheets("Tracker").Select 'Main Sheet that is being Updated
        ActiveCell.Offset(0, 10).Copy
    
        Windows(mdt).Activate
        Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
        ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
            :=False, Transpose:=False
            
        Windows(ws).Activate
        Sheets("Tracker").Select 'Main Sheet that is being Updated
        ActiveCell.Offset(0, 19).Copy
    
        Windows(mdt).Activate
        Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
        ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
            :=False, Transpose:=False
            
        Windows(ws).Activate
        Sheets("Tracker").Select 'Main Sheet that is being Updated
        ActiveCell.Offset(0, 20).Copy
    
        Windows(mdt).Activate
        Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
        ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
            :=False, Transpose:=False
        ActiveCell.Offset(1, -5).Select
        
[COLOR=#ff0000]NextProp:
        If Err.Number <> 0 Then
            Windows(mdt).Activate
            Sheets("Deal Tracker").Select
            ActiveCell.Offset(1, 0).Select 'Looks up Porperty Name
        End If[/COLOR]
    Loop
    Application.ScreenUpdating = True
End Sub

Cheers,

tonyyy
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top