Solved

VBA coding

Posted on 2013-05-22
3
294 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 250 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 250 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

821 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