Object variable or with block variable is not set

kaiko_k

New Member
Joined
Jan 16, 2018
Messages
1
I'm having a problem with a piece of code. I have searched and tried various solutions but nothing seems to work.
I also don't understand why sometimes this code works perfectly, for example when restarting excel and opening workbook again. And then after I have run few other codes (workbook contains many) I keep getting "Object variable or with block variable is not set" error.

Debug shows I have a problem with row "Worksheets("Leping").Range("A1:A10000").Find("lisa_kaasomanik", lookat:=xlPart).Activate"

Basically what I'm trying to do is to activate a certain cell and then insert new row according to active cell while copying formats from another row.

Can anyone please guide me to a solution?

Code:
Sub InsertRow()


Application.ScreenUpdating = False
ActiveSheet.Unprotect


Worksheets("Leping").Range("A1:A10000").Find("lisa_kaasomanik", lookat:=xlPart).Activate


       
    ActiveCell.Offset(0, 0).EntireRow.Insert
    ActiveCell.Offset(-1, 0).EntireRow.Copy
    ActiveCell.Offset(0, 0).Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    
    ActiveSheet.Protect


Application.ScreenUpdating = True


End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
If you use the Find statement, you must also consider the case where the search value was not found.

Code:
Sub InsertRowv()
Dim FC, Fcr As Long
Application.ScreenUpdating = False
Worksheets("Leping").Unprotect
Set FC = Worksheets("Leping").Range("A1:A10000").Find("lisa_kaasomanik", LookIn:=xlValues, lookat:=xlPart)
    If FC Is Nothing Then
        MsgBox "Not Found"
    Else
        Fcr = FC.row
        Rows(Fcr).EntireRow.Insert
        Rows(Fcr - 1).EntireRow.copy
        Rows(Fcr).EntireRow.PasteSpecial Paste:=xlPasteFormats
        Application.CutCopyMode = False
        Worksheets("Leping").Protect
        Application.ScreenUpdating = True
    End If
End Sub

And, the find statement inherits the search method that the user performed manually the next time.
The following code is without the find statement.

Code:
Sub test()
    Dim i As Long, j As Long
    Dim rng As Range, x
    
    Application.ScreenUpdating = False
    Worksheets("Leping").Unprotect
    
    With Sheets("Leping")
        Set rng = .Range("A1:A10000")
        x = rng
        For j = LBound(x) To UBound(x)
            If x(j, 1) Like "*lisa_kaasomanik*" Then
                .Rows(j).EntireRow.Insert
                .Rows(j - 1).EntireRow.copy
                .Rows(j).EntireRow.PasteSpecial Paste:=xlPasteFormats
                Application.CutCopyMode = False
                .Protect
                Exit For
            End If
        Next
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
The reason that it sometimes works & sometimes doesn't, is that you have only specified one of the parameters. Excel remembers these parameters every time you use Find.
e.g The original default (when you open xl) is MatchCase=False, but if you use Find with MatchCase=True, the default becomes True.
Whenever using Find, or Replace it is always a good idea to specify any parameters that may affect what you're looking for.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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