Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA coding

Posted on 2013-05-22
3
Medium Priority
?
322 Views
Last Modified: 2013-05-22
I get a "Run Time Error 91 - Object variable or With block variable not set" at Line 24 when I run the below code. What is my mistake? Thanks.

Private Sub DeleteRowsWithSmallAmountAtIssue()
'   Delete rows that have a value of Delete in Column B.
    Dim rng1 As Range
    Dim rng2 As Range
    Dim lngRow As Long
    'Application.ScreenUpdating = False
    If [b2] <> vbNullString Then
        Set rng1 = Range([b2], [b1].End(xlDown))
    Else
        Set rng1 = [b1]
    End If
    For lngRow = rng1.Rows.Count To 2 Step -1
        If (Cells(lngRow, "B") = "Delete") Then
            'Rows(lngRow).EntireRow.Delete
            If rng2 Is Nothing Then
                Set rng2 = Rows(lngRow)
            Else
                Set rng2 = Union(rng2, Rows(lngRow))
            End If
        Else
        '    If Left$(Cells(lngRow, "c"), 2) <> "CV" Then Rows(lngRow).EntireRow.Delete
        End If
    Next
    rng2.EntireRow.Delete
    Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:carlosab
3 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 1000 total points
ID: 39187658
Your for loop does not Guarantee that rng2 will be assigned a value.

rng2 is only assigned a value with certainty if the following is true:

If (Cells(lngRow, "B") = "Delete") Then

Open in new window

0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 1000 total points
ID: 39187693
It's a fairly simple fix though:

If Not rng2 Is Nothing Then rng2.EntireRow.Delete

Open in new window

0
 

Author Closing Comment

by:carlosab
ID: 39187866
Thank you both. I've now discovered that the problem is actually that the For sequence was being skipped. I've assigned points here for the correct answer and posted a related question to: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28135796.html
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question