?
Solved

VBA coding

Posted on 2013-05-22
9
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39187916
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 56

Expert Comment

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

~bp
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39187943
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
Industry Leaders: 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!

 
LVL 19

Expert Comment

by:Ken Butters
ID: 39188239
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
 

Author Comment

by:carlosab
ID: 39188248
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
ID: 39188329
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
ID: 39188386
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 2000 total points
ID: 39188537
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
ID: 39188633
Perfect. Thank you again.
0

Featured Post

Industry Leaders: 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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

752 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