Solved

VBA coding

Posted on 2013-05-22
9
289 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 54

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Independent Software Vendors: 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

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…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

740 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