VBA to delete rows based on a condition in another worksheet

McTock

New Member
Joined
Jan 27, 2014
Messages
5
I am working with a workbook with three main sheets in it:
1. Summary sheet
2. Raw data sheet
3. Area list

The main purpose of this workbook is to create a list of customer contracts that can be tracked and edited by the contract auditors. On the Area list all the areas the specific auditor is responsible for are marked with a "Yes" and the others with a "No". The raw data gets transferred to the Summary sheet by the following:

Sheets("Raw").Range("d2:d100000", "e2:f100000").Copy Sheets("Summary").Cells(Rows.Count, 1).End(xlUp).Offset(1, 1)

where column "D" is the area name and "E:F" the customer reference numbers. This, however copies all the areas to the Summary sheet and not only those the auditor is responsible for. I now need to approach this in one of two ways;

1. either only copy the required areas to the summary listing by setting a condition testing the Area list for a "Yes" and then only copy those lines from the raw data sheet, or
2. copy all lines to Summary as is done now and then conditionally delete all lines that belongs to the "No" group.

I have tried Union, Concatenate, VLookup, IF and a lot more and have scanned the Internet for all possible solutions, but no succes. My latest effort to conditionally delete the unwanted rows:


***********************************
Set Sh1 = Sheets("ServiceAreas")
Set Sh2 = Sheets("Summary")

Application.ScreenUpdating = False


For n1 = 1 To LastRowcheck

nextrow = Sh2.Cells(Rows.Count, "A").End(xlUp).Row + 1

If ActiveCell.FormulaR1C1 = "=VLOOKUP(B2,ServiceAreas!C:D,1,false)" = True Then

With Sh1
lastrow = .Cells(Rows.Count, "C").End(xlUp).Row
lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
.AutoFilterMode = False
Set rng = .Range("A1", .Cells(lastrow, lastcol))
rng.AutoFilter field:=4, Criteria1:="No"
rng.Offset(1, 0).SpecialCells(1).Copy Sh2.Range("A" & nextrow)
rng.Offset(1, 0).EntireRow.Delete shift:=xlUp
.AutoFilterMode = False

End With

End If


*************************
How do I get this right, then?

Thanking you in advance.

McTock
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Have you considered using an autofilter to filter out the "no"'s then copy and paste the visible cells to the summary sheet? It's likely to be faster than what you are considering.
 
Upvote 0
Have you considered using an autofilter to filter out the "no"'s then copy and paste the visible cells to the summary sheet? It's likely to be faster than what you are considering.


[QUTE=McTock]JoeMo, yes I did but also did not have much success. The problem that I must bridge, is that nothing gets copied from the Area sheet onto the Summary sheet. The Area sheet only lists all the areas I must work with.

Thank you for the reply. I will see if I have saved the autofilter attempt somewhere and try it again.[QUOTE)]
 
Upvote 0
Sorry JoeMo, but I could not find the script and is still strugling to delete all the rows associated with the "No" in the area sheet.

Any suggestions?
 
Upvote 0
Upvote 0
Excel 2010
A
B
C
D
1
Region
Customer Service Area
Service Area
Selection
2
CENTRAL
Klerksdorp
ASKHAM
Yes
3
CENTRAL
Klerksdorp
BANKSDRIFT
No
4
CENTRAL
Klerksdorp
BATHO BATHO
No

<TBODY>
</TBODY>



Cell Formulas
RangeFormula
B1Service Area
B2BARKLEY WEST
B3Petrusville
B4Petrusville
B5Petrusville
B6Petrusville
C1Customer Account Number
C25338827561
C35030103738
C45042267420
C55047044370
C65060881367





Cell Formulas
RangeFormula
D1Service Area
D2Kimberley
D3JACOBSDAL
D4BENONI:KATLEHONG LPU CUSTOMER
D5DOUGLAS
D6BETHLEHEM
D7KIMBERLEY
E1Customer Account Number
E26023333046
E36994603809
E46998728021
E55209968205
E68562968516
E75702937632
 
Last edited:
Upvote 0
Sorry JoeMo, but the administrator logged me out before the posting was done and edited. What you see posted is not what I actually wanted to post, but we must now make it work.
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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