Link to home
Start Free TrialLog in
Avatar of Euro5
Euro5Flag for United States of America

asked on

VBA delete rows based on phrase

I have a vba code to name ranges.

However, to work correctly, the phrase it identifies "Continental U.S. Ground" has to be 2 lines above the phrase "Weight (in Lbs )".
Is there any way to identify these two phrases in Column A and remove any additional lines between them?

Needs to be in column A on sheet "Net Rates 1"
"Continental U.S. Ground"
-EMPTY ROW-
"Weight (in Lbs )"

Help??
Avatar of Ramesh V
Ramesh V

Can you send me data?
Avatar of Euro5

ASKER

The data could be any number of rows in between.
I think I also need to run remove merge and wrap also to get this to work consistently, can you help me do that?

THANK YOU!!
sample.xlsx
Avatar of Glenn Ray
This code will reset the word wrap and merging anchored on cell A2 and then remove all rows but one between A2 and the top row of the table.

Option Explicit
Sub Clear_Rows()
    With Range("A2")
        .HorizontalAlignment = xlGeneral
        .UnMerge
        .WrapText = False
    End With
    Range("A3").Select
    Do Until ActiveCell.Offset(1, 0).Value = "Weight (in Lbs )"
        ActiveCell.EntireRow.Delete
    Loop
End Sub

Open in new window


Regards,
-Glenn
Avatar of Euro5

ASKER

Glenn,
The data will not be in the same cells. They could be anywhere in the sheet.
I'm not certain, but it appears that the above assumes that the "Continental U.S. Ground"  is in a specific cell?
Avatar of Euro5

ASKER

Not anywhere in the sheet - they could be in any ROW - the phrases are always in Column A.
So, "Continental U.S. Ground" (and it's merged/wrapped cells) could be on any row in column A - then followed further down the sheet by the table?

Could you post another variation of your example that shows other possible configuration(s)?  I'm sure it's an easy mod to the example code I provided.

-Glenn
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Euro5

ASKER

Wow, perfect! Thanks so much!
Avatar of Euro5

ASKER

What if there is an error? Can we handle that?
What kind of error?  Probably best to ask a new question and post a link to this question.
Avatar of Euro5

ASKER

aikimark - sorry, yes you are right. Asked New question is - ID: http:Q_28687555