Solved

Macro not woking

Posted on 2015-02-01
4
84 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 45

Accepted Solution

by:
Martin Liss earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, guys lot of flickering, but it works
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now