Code fails with runtime error 91 in some case.

lavinius

New Member
Joined
Jan 8, 2013
Messages
20
Hi,

Need some help with my current code. when run code on some sheets in a MainWorkbook, it works just fine. but when run in other sheets with longer ranges to match it fails with runtime error 91. highlights the copy paste rows in code. but when i the stop code and close vba editor the macro has still done its job.

In other cases when some of the ranges C F and I are empty, say range F is empty, it fails alltogether.
Also, is there a way to post example files here? Would make it easier to show what goes wrong.

Code is

Code:
Option Explicit

Sub Import()
Dim fname As String


Dim Crng As Range, Frng As Range, Irng As Range


Set Crng = ActiveSheet.Range("C7")
Set Crng = Range(Crng, Crng.End(xlDown))


Set Frng = ActiveSheet.Range("F7")
Set Frng = Range(Frng, Frng.End(xlDown))


Set Irng = ActiveSheet.Range("I7")
Set Irng = Range(Irng, Irng.End(xlDown))


fname = ActiveSheet.Name


Workbooks.Open FileName:=Range("M2")
Sheets(fname).Select


Dim Erng As Range, aCell As Range, bCell As Range


Set Erng = ActiveSheet.Range("E5")
Set Erng = Range(Erng, Erng.End(xlDown))


For Each aCell In Crng
    Set bCell = Erng.Find(What:=aCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not aCell Is Nothing Then
        aCell.Offset(, -1) = bCell.Offset(, -1)
        aCell.Offset(, 1) = bCell.Offset(, -2)
    End If
    Next


For Each aCell In Frng
    Set bCell = Erng.Find(What:=aCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not aCell Is Nothing Then
        aCell.Offset(, -1) = bCell.Offset(, -1)
        aCell.Offset(, 1) = bCell.Offset(, -2)
    End If
    Next


For Each aCell In Irng
    Set bCell = Erng.Find(What:=aCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not aCell Is Nothing Then
        aCell.Offset(, -1) = bCell.Offset(, -1)
        aCell.Offset(, 1) = bCell.Offset(, -2)
    End If
    Next
    
ActiveWindow.Close


End Sub

All help is appriciated!

Cheers
 

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