[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Macro not woking

Posted on 2015-02-01
4
Medium Priority
?
94 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
[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
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Martin Liss earned 1000 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:Michael Fowler
Michael Fowler earned 1000 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 49

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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!
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

649 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