Solved

VBA coding

Posted on 2013-05-22
3
265 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now