Solved

VBA coding

Posted on 2013-05-22
9
249 Views
Last Modified: 2013-05-22
When I step through this code, it goes from Line ___ directly to Line 27. Why?

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
    If Not rng2 Is Nothing Then rng2.EntireRow.Delete
    Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:carlosab
  • 5
  • 3
9 Comments
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
looks like you are setting rng1 to a value at the top of the document... you are using xlDown when I think you mean to use xlUp on line 8.

So in effect you are starting a the top of the document... and moving upwards. (step -1).

You really want to to start at the bottom and move upwards... which if you change line 8... I think that will do what you want.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
What line does it go from, you only showed underscores?

~bp
0
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
Btw... if you select debug from the Visual Basic Editor menu... it shows you the keys to hit to trace this code.   F8 is the key that will start debugging this code... that would make it very easy for you to trace the code / inspect variables etc... to determine what's going on.

The debugging built into VBE is outstanding.  Definitely worth your time to learn if you are going to be working with VBA very much.
0
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
looked at line 8 a little closer...

I think this will work as you intended:

Set rng1 = Range("B:B").End(xlDown)

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:carlosab
Comment Utility
Sorry, I should have said that it goes from Line 12 to Line 24. I have attached a file with some sample data and with the code. All that I am trying to do is remove all rows where the word Delete is in column B. Thanks.
Sample-Workbook.xlsm
0
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
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
    
    Set rng1 = Cells(Rows.Count, "B").End(xlUp)
    
     For lngRow = rng1.Row To 2 Step -1
        If (Cells(lngRow, "B") = "Delete") Then
            Rows(lngRow).EntireRow.Delete
        End If
        
    Next
    'Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:carlosab
Comment Utility
This code works great in isolation. But when I put it into my full spreadsheet and run it, I get a Run-time error '13': Type mismatch at Row 11. The value of rng1 at that point is Error 2042.
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
Comment Utility
Error 2042 means the cell contains "#NA".

you can do something like this to check for that...

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
    
    Set rng1 = Cells(Rows.Count, "B").End(xlUp)
    
     For lngRow = rng1.Row To 2 Step -1
        If WorksheetFunction.IsNA(Cells(lngRow, "B")) Then
        ' do something... you can delete the row or ignore it or whatever
        ElseIf (Cells(lngRow, "B") = "Delete") Then
            Rows(lngRow).EntireRow.Delete
        End If
        
    Next
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Closing Comment

by:carlosab
Comment Utility
Perfect. Thank you again.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
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 …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

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

13 Experts available now in Live!

Get 1:1 Help Now