combine macro

sarfarazbutt

Board Regular
Joined
Jun 10, 2009
Messages
55
Below macro is deleting empty row, I would like to have combine macro which can delete also the rows if text found in first cell "Not found" and top of this row also delete. i.e if row 40 found "Not found" text delete this row and 39 row should be delete. any idea




Option Explicit

Sub DeleteEmptyRows()

Dim LastRow As Long
Dim Cnt As Long
Dim r As Long

Application.ScreenUpdating = False

LastRow = Cells.Find(what:="*", _
after:=Range("A1"), _
LookIn:=xlFormulas, _
lookat:=xlPart, _
searchorder:=xlByRows, _
searchdirection:=xlPrevious, _
MatchCase:=False).Row

For r = LastRow To 1 Step -1
If WorksheetFunction.CountA(Rows(r)) = 0 Then
Rows(r).Delete
Cnt = Cnt + 1
End If
Next r

Application.ScreenUpdating = True

MsgBox Cnt & " rows were deleted.", vbInformation

End Sub
 
this code is deleting only empty row

Excel Workbook
ABCD
1Nb. #Room Type
2Res NNameCompany/Agency/Source/GroupArrival
3Cancellations ForSaturday, January 01, 2011
4Not found
5Cancellations ForSunday, January 02, 2011
6Not found
7Cancellations ForMonday, January 03, 2011
8Not found
9Cancellations ForTuesday, January 04, 2011
10Not found
11Cancellations ForWednesday, January 05, 2011
12Not found
13Cancellations ForThursday, January 06, 2011
14Not found
15Cancellations ForFriday, January 07, 2011
16Not found
17Cancellations ForSaturday, January 08, 2011
18Not found
19Cancellations ForSunday, January 09, 2011
20Not found
21Cancellations ForMonday, January 10, 2011
22194,499Al Alfelasi, NadaBooking.com01/08/
23Cancellations ForTuesday, January 11, 2011
24Not found
25Cancellations ForWednesday, January 12, 2011
26Not found
27Cancellations ForThursday, January 13, 2011
28Not found
29Cancellations ForFriday, January 14, 2011
30Not found
31Cancellations ForSaturday, January 15, 2011
32Not found
33Cancellations ForSunday, January 16, 2011
34Not found
35Cancellations ForMonday, January 17, 2011
MOEV_CXL_BOOKINGS.RPT
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try changing this line...
If LCase(Range("A" & r).Value) = "not found" Then

To this...
If LCase(Trim(Range("A" & r).Value)) = "not found" Then
 
Upvote 0
In the code, you kept "not found" all in lower case? It has to be all lower case in the code. It doesn't matter what the case the data is.

Code:
If LCase(Trim(Range("A" & r).Value)) = "[COLOR="Red"]not found[/COLOR]" Then
 
Upvote 0
This should simplify thing a liittle. It will move thru the used range one row at a time and delete the row that meets the condition. If the row needs to be deleted, it will be, but the row counter wiil not move forward. This makes sure that multiple row in sequence that meet the condition will be deleted

Code:
Sub DeleteEmptyRows()
    Dim Cnt    As Long
    Dim r      As Long
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    Set ws = ThisWorkbook.Worksheets(1)
    
    r = 2
    Do While r <= ws.UsedRange.Rows.Count
        If WorksheetFunction.CountA(Rows(r)) = 0 Or (LCase(Range("A" & r).Value) = "not found") Then
            Rows(r).Delete
            Cnt = Cnt + 1
        Else
            r = r + 1
        End If
    Loop
    Application.ScreenUpdating = True
    
    MsgBox Cnt & " rows were deleted.", vbInformation
    
End Sub
 
Upvote 0
This should simplify thing a liittle. It will move thru the used range one row at a time and delete the row that meets the condition. If the row needs to be deleted, it will be, but the row counter wiil not move forward. This makes sure that multiple row in sequence that meet the condition will be deleted

Code:
Sub DeleteEmptyRows()
    Dim Cnt    As Long
    Dim r      As Long
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    Set ws = ThisWorkbook.Worksheets(1)
    
    r = 2
    Do While r <= ws.UsedRange.Rows.Count
        If WorksheetFunction.CountA(Rows(r)) = 0 Or (LCase(Range("A" & r).Value) = "not found") Then
            Rows(r).Delete
            Cnt = Cnt + 1
        Else
            r = r + 1
        End If
    Loop
    Application.ScreenUpdating = True
    
    MsgBox Cnt & " rows were deleted.", vbInformation
    
End Sub
But does it produce the desired results? Run it on the sample data in post #9 and compare your results to the desired results shown in that post.
 
Upvote 0
You right Peter SSs. I missed the fact the the "Cancellation For:" rown needs to be deleted if the following row does not have a numeric number.

That's for pointing me back at post #9
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top