VBA - If range is beyond the active worksheet

RuinAerlin

New Member
Joined
Apr 8, 2016
Messages
10
Hi All,

I've put a sample of my code below (it repeats for every 6th column up to GW). I get a run time error '91' Object Variable or With Block variable not set when the worksheet does not extend that many columns e.g. on my 3rd tab it only goes up to EN and I debugger highlights the line;

Lastrow = Range("EO:EO").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

What I am trying to do is if the worksheet doesn't extend that far, go to workinprogress.

Code:
Dim i As Integer
    For i = 2 To Worksheets.Count
        Sheets(i).Select
Dim Lastrow As Long
    Dim LR As Long
If Not Range("G:G") Is Nothing Then
 Lastrow = Range("G:G").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 End If
Range("G4:K" & Lastrow).Select
    Selection.Copy
LR = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A" & LR).Offset(1).Select
    ActiveSheet.Paste
On Error GoTo workinprogress
If Not Range("M:M") Is Nothing Then
 Lastrow = Range("M:M").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 End If
Range("M4:Q" & Lastrow).Select
    Selection.Copy
LR = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A" & LR).Offset(1).Select
    ActiveSheet.Paste
On Error GoTo workinprogress

workinprogress:
Lastrow = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A4:E" & Lastrow).Select
    Selection.Copy
    Sheets("WIP").Select
 Lastrow = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A" & Lastrow).Select
    ActiveSheet.Paste
    Next i
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about something like this?
Code:
Dim i As Integer
Dim Lastrow As Long
Dim LR As Long
Dim rngFnd As Range
Dim rngLook As Range

    For i = 2 To Worksheets.Count
    
        With Sheets(i)
            Set rngLook = .Range("G:G")
            Do
                Set rngFnd = rngLook.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
            
                If Not rngFnd Is Nothing Then
                    Lastrow = rngFnd.Row
                    .Range(.Cells(4, rngLook.Column), .Cells(Lastrow, rngLook.Column + 5)).Copy .Range("A" & Rows.Count).End(xlUp).Offset(1)
                End If
                
                Set rngFnd = rngFnd.Offset(, 6)
            Loop Until rngFnd Is Nothing
        
workinprogress:
            Lastrow = .Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("A4:E" & Lastrow).Copy Sheets("WIP").Range("A" & Rows.Count).End(xlUp).Offset(1)
            
        End With
        
    Next i
 
Upvote 0
I think it's stuck in an endless loop somewhere after trying to run that. I've been waiting over half an hour.
 
Upvote 0
Oops, my fault - the Offset(,6) should be for rngLook not rngFnd.:eek:
Code:
                Set rngLook = rngLook.Offset(, 6)
            Loop Until rngFnd Is Nothing
 
Upvote 0
It's copying data in G:K repeatedly. Unfortunately I cannot follow your code to work out why this is happening. I like the premise of it though!

Aha that works perfectly thank you!!!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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