Solved

VBA coding

Posted on 2013-05-22
9
295 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 55

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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 create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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 …

726 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