Solved

Macro not woking

Posted on 2015-02-01
4
88 Views
Last Modified: 2015-02-02
Not very familiar with excel 2000
First time i used this macro it worked on another worksheet, but not this one

Public Sub RemoveRows()
Dim SearchString As String
Dim i As Long
SearchString = "Authorization"
i = 1

Do While i <= ThisWorkbook.ActiveSheet.Range("A1").CurrentRegion.Rows.Count
Debug.Print ThisWorkbook.ActiveSheet.Cells(i, 3).Text
    If InStr(1, ThisWorkbook.ActiveSheet.Cells(i, 3).Text, SearchString, vbTextCompare) > 0 Then
        ThisWorkbook.ActiveSheet.Cells(i, 1).EntireRow.Delete
    Else
        i = i + 1
    End If
Loop
Excel.jpgDebug giving these results
33
39
free
44
35
trying to remove row if 3rd column contains the string "Authorization"
what is the problem ?
0
Comment
Question by:isnoend2001
  • 2
4 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 250 total points
ID: 40583203
Anytime you delete rows from a range you need to loop backward through the range, so

For ThisWorkbook.ActiveSheet.Range("A1").CurrentRegion.Rows.Count to 1 Step -1
Debug.Print ThisWorkbook.ActiveSheet.Cells(i, 3).Text
    If InStr(1, ThisWorkbook.ActiveSheet.Cells(i, 3).Text, SearchString, vbTextCompare) > 0 Then
        ThisWorkbook.ActiveSheet.Cells(i, 1).EntireRow.Delete
    Else
        i = i + 1
    End If
Next

Open in new window


If that doesn't work then please attach your workbook.
0
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 250 total points
ID: 40583261
Looping backward through the range is the right technique but here is a working example

Public Sub RemoveRows()
    Dim SearchString As String
    Dim i As Long
    
    SearchString = "Authorization"
    
    For i = Range("A1").CurrentRegion.Rows.Count To 1 Step -1
        Debug.Print Cells(i, 3).Text
        If InStr(1, Cells(i, 3).Text, SearchString, vbTextCompare) > 0 Then
            Cells(i, 1).EntireRow.Delete
        End If
    Next
End Sub

Open in new window

0
 

Author Closing Comment

by:isnoend2001
ID: 40583389
Thanks, guys lot of flickering, but it works
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40584246
To stop the flickering put

Application.ScreenUpdating = False at the start of the code and Application.ScreenUpdating = True at the end.

In any case you're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

828 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