Link to home
Start Free TrialLog in
Avatar of carlosab
carlosab

asked on

VBA coding

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

Avatar of Ken Butters
Ken Butters
Flag of United States of America image

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.
Avatar of Bill Prew
Bill Prew

What line does it go from, you only showed underscores?

~bp
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.
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

Avatar of carlosab

ASKER

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
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

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.
ASKER CERTIFIED SOLUTION
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect. Thank you again.