"Error 91"

TFCJamieFay

Active Member
Joined
Oct 3, 2007
Messages
480
Afternoon all,

I'm trying to write a piece of code that will sort numbers depending the first digit but I keep getting "Run-time error '91' - Object variable or With block variable not set" on the line:

"mCell = ActiveCell.Offset(columnoffset:=2, rowoffset:=0).Value"

But when I hover my mouse over the line of code it gives the correct value, so it seems to be finding the correct cell and extracting the correct value. I'm confused!!

My code is below:

Sub test2()

Dim MTN, BTN As String
Dim bCell, mCell As Object

lastrow = Range("A" & Rows.Count).End(xlUp).Row

Range("D2:D" & lastrow).Select

For Each cell In Selection
cell.Select
mCell = ActiveCell.Offset(columnoffset:=2, rowoffset:=0).Value
bCell = ActiveCell.Offset(columnoffset:=1, rowoffset:=0).Value
MTN = mCell.Value
BTN = bCell.Value
Select Case Left(cell, 1)
Case "7"
If MTN = "" Then
mCell.Value = cell.Value
cell.Value = ""
End If
Case "1", "2"
If Left(bCell, 1) = "7" Then
mCell.Value = bCell.Value
bCell.Value = ""
End If
Case Else
If cell.Value = "" And Left(bCell, 1) = "1" Or Left(bCell, 1) = "2" Then
cell.Value = bCell.Value
End If
End Select
Next

End Sub

Many thanks,

Jamie
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Jamie

Do you really need to select?
Code:
For Each cell In Range("D2:D" & lastrow)
       mCell = cell.Offset(columnoffset:=2, rowoffset:=0).Value
       bCell = cell.Offset(columnoffset:=1, rowoffset:=0).Value
' etc
 
Upvote 0
You declare mCell as an Object but then try and pass it the value of a cell, which makes no sense. I suspect you wanted:
Code:
Set mCell = ActiveCell.Offset(columnoffset:=2, rowoffset:=0)
also, you should be aware that if you use this:
Code:
Dim bCell, mCell As Object
you have only declared mCell as an Object. bCell is the default Variant type because you did not specify its type. You should use:
Code:
Dim bCell as Object, mCell As Object
and similarly for your string declarations.
 
Upvote 0
Hi there,

Wasn't sure where to put this as this is my 1st post. Didn't think I needed to create a new thread as this is a part of my problem anyway.

I am trying to do a loop with what seems to be a very simple piece of code and I keep getting the runtime error 91.

I have this piece which runs fine until the end then the part in red is what's highlighted for debugging.


Rich (BB code):
Sub test()                
 Do
  With Application.FindFormat.Font
     .Name = "Arial"
     .FontStyle = "Regular"
     .Size = 8
     .ColorIndex = xlAutomatic
   End With
        Range("A4").Select
           Cells.Find(what:="$", after:=ActiveCell, LookIn:=xlFormulas, lookat:= _
             xlPart, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False _
                 , searchformat:=True).Activate
            Range(Selection, Selection.End(xlDown)).Select
            Selection.EntireRow.Select
            Selection.Delete Shift:=xlUp
Loop
                
End Sub
The amount of times it needs to run will differ each time so the other thing I was trying to figure out is how to add a MsgBox at the end to let me know there's no more to find and delete and to obviously exit the loop. The code ran fine before I tried to put it into a loop!!!!!

I hope this makes sense.

Thanks for any help with this

Darrin
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,453
Members
448,898
Latest member
drewmorgan128

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