Object variable not set (Error 91) occurs part way through running code

Westbury

Board Regular
Joined
Jun 7, 2009
Messages
139
I have a code to run through columns and staggered rows to collect data into an array. The staggered layout becomes one row in the array. It runs successfully for the first 2 groups of data but towards the end of the 3rd group it produces an error message "Object variable not set (Error 91). It trips on the line in red below.

What might be the problem?

thanks


VBA Code:
With ActiveSheet
For r = r + 1 To LastRow

    For i = Counter To LastRow Step 1
            If Cells(Counter, 1).Value <> Cells(Counter + 1, 1).Value Then
                Exit For
            End If
          
        Counter = Counter + 1
      
    Next i
  
'group end row
EndRow = Counter

For c = 1 To 12  'columns A to L
  
    Set startCell = Cells(StartRow, c)
           
    Set Rng = Range(Cells(StartRow, c), Cells(EndRow, c))
  
         If WorksheetFunction.CountA(Rng) = 0 Then
       
         Aptdatarray(r, c) = ""
       
         Else
       
         [COLOR=rgb(184, 49, 47)]Aptdatarray(r, c) = Rng.Find(What:="*", After:=Cells(StartRow, c)).Value  [/COLOR] 
           
         End If
    
   Next c

    StartRow = Counter + 1
    Counter = Counter + 1

        If StartRow >= LastRow Then
        Exit Sub
        End If

Next r

End With
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You will get that error if all cells in Rng are empty. Im guessing thats it.
 
Upvote 0
I included the lines

VBA Code:
 If WorksheetFunction.CountA(Rng) = 0 Then
       
         Aptdatarray(r, c) = ""

to try and trap that condition and it appears to work as I can see the array load 3 cells with "" before it trips.

Is there a better way to catch blanks cells? or should I just skip the blanks?I've been trying to ensure that each row of the array is complete; does that matter?
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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