MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 24th, 2002, 03:32 PM   #1
thexfactor44
New Member
 
Join Date: Feb 2002
Posts: 26
Default

you're all probably tired of hearing about this:
This code has been given to me to search sheet 2's column A for any value in sheet 1's Column A and delete the entire row in Sheet 2 if found. the issue is that it consumes way too much memory and crashes xl(2k). is there a way to speed it up or make it use less mem? thanks!
(FYI: Sheet 1's column A, or the value to search for is 5K deep, Sheet 2 is the sheet to search in and is 25K deep)


Sub DeleteRedundants()

Dim i As Integer, j As Integer

'Loop through first sheet
For i = 1 To 60000

'Exit if blank cell found
If Sheets("Sheet1").Cells(i, 1).Value = "" Then Exit For

'Loop through second sheet
For j = 1 To 60000
'Exit if blank cell found
If Sheets("Sheet2").Cells(j, 1).Value = "" Then Exit For
'If duplicate found, delete the row and exit
If Sheets("Sheet1").Cells(i, 1).Value = Sheets("Sheet2").Cells(j, 1).Value Then
Sheets("Sheet2").Cells(j, 1).EntireRow.Delete
Exit For
End If

Next

Next

End Sub
thexfactor44 is offline   Reply With Quote
Old May 24th, 2002, 03:52 PM   #2
Jack in the UK
Board Regular
 
Join Date: Feb 2002
Posts: 3,065
Default

I can’t provide the answer with new codes, but I can advise why the script runs slow

You are pushing Excel and beautiful as she is that shopping heavy and so the loop means she carry’s extra beers for us boys. Thus a little later getting home. OK a childish explanation but you understand

The loop is slow and always will be.

I use FIND and act on that find IE do them all at the same time.

This mean fast codes, just a different way to work, remember slow is only if you MUST be super fast, few seconds matters little.

__________________
Free Excel based Web Toolbar available here.

Jack in the UK
J & R Excel Solutions
"making Excel work for you"
Jack in the UK is offline   Reply With Quote
Old May 24th, 2002, 04:00 PM   #3
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Try the following,

Code:
Sub test()
Dim Rng1 As String, lastrow As Long

Application.ScreenUpdating = False

With Sheets("Sheet1")
Rng1 = Intersect(.UsedRange, .Range("A:A")).Address(True, True, xlR1C1)
End With

With Sheets("Sheet2")
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
.Range(.Cells(1, 2), .Cells(lastrow, 2)) = "=MATCH(RC[-1],Sheet1!" & Rng1 & ",0)"
.Rows(1).Insert
    With .Cells(1, 2)
        .Value = "Temp"
        .AutoFilter Field:=2, Criteria1:="<>#N/A"
    End With
.UsedRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Columns(2).Clear
End With

End Sub
This assumes that there is nothing in column B in sheet 2. You would have to adjust a few things above to account for that (easily done, but please report back any problems first).

Bye,
Jay

EDIT: The rows have to match as well? If that is the case, try...

Code:
Sub test2()
Dim lastrow As Long

With Sheets("Sheet2")
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
.Range(.Cells(1, 2), .Cells(lastrow, 2)) = "=IF(RC[-1]=Sheet1!RC[-1],1,0)"
.Range(.Cells(1, 2), .Cells(lastrow, 2)).Copy
.Range(.Cells(1, 2), .Cells(lastrow, 2)).PasteSpecial (xlValues)
.Rows(1).Insert
    With .Cells(1, 2)
        .Value = "Temp"
        .AutoFilter Field:=2, Criteria1:="<>1"
    End With
.UsedRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Columns(2).Clear
End With
MsgBox "Done!"

End Sub
[ This Message was edited by: Jay Petrulis on 2002-05-24 15:09 ]
Jay Petrulis is offline   Reply With Quote
Old May 24th, 2002, 04:19 PM   #4
thexfactor44
New Member
 
Join Date: Feb 2002
Posts: 26
Default

while the above code works as well, quite nicely when I my columns are approx. 100 deep, it freezes up the program when i try to run it searching within a coulmn 5000 sells deep (sheet 1) into a column 25000 deep (sheet 2). any suggetions?
thexfactor44 is offline   Reply With Quote
Old May 24th, 2002, 04:50 PM   #5
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

I tried this with a full column of data (65,536 points) on Sheet1 and 25,000 points on Sheet2.

Code:
Sub test()
Dim Rng1 As String, lastrow As Long

Application.ScreenUpdating = False

With Sheets("Sheet1")
Rng1 = Intersect(.UsedRange, .Range("A:A")).Address(True, True, xlR1C1)
End With

With Sheets("Sheet2")
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row

Application.Calculation = xlCalculationManual
.Range(.Cells(1, 2), .Cells(lastrow, 2)) = "=MATCH(RC[-1],Sheet1!" & Rng1 & ",0)"
.Range(.Cells(1, 2), .Cells(lastrow, 2)).Copy
.Range(.Cells(1, 2), .Cells(lastrow, 2)).PasteSpecial (xlValues)
Application.Calculation = xlCalculationAutomatic

.Rows(1).Insert
    With .Cells(1, 2)
        .Value = "Temp"
        .AutoFilter Field:=2, Criteria1:="<>#N/A"
    End With
.UsedRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Columns(2).Clear
End With

End Sub
There are two bottleneck lines.

1. The line that loads the =MATCH function to the worksheet.

2. The line that deletes the rows.

I turned off the autocalc for the first one to speed it up as much as possible.

Will try to load it all into an array, too.

Bye,
Jay

EDIT: It shouldn't bomb your computer. Just give it time to run (depending on the speed of your machine, possibly even 5-6 minutes) and you will get what you need. I just did 45K on sheet1 and 9K on sheet2 in under 2 minutes.

The original For-Next loop shouldn't bomb your computer, either, although it will be slow as well.

[ This Message was edited by: Jay Petrulis on 2002-05-24 15:56 ]
Jay Petrulis is offline   Reply With Quote
Old May 24th, 2002, 10:32 PM   #6
robfritts
New Member
 
Join Date: Apr 2002
Location: KC, MO
Posts: 18
Default

Just an observation here... But I've been working with conditionally deleting rows and found two things that sped it up significantly for me...

Turn off screen updates (autoupdate something or other) and instead of .DeleteRow, I just used .ClearContents and it ran many many times faster although I'm not entirely sure why.
robfritts is offline   Reply With Quote
Old May 25th, 2002, 01:15 PM   #7
Jack in the UK
Board Regular
 
Join Date: Feb 2002
Posts: 3,065
Default

Clear contents will out strip delete simple as there is not cells movement simple as that, delete row 2 and 65536 cells less 2 gog up one and an extra slip in the bottom making 65536, thus all even

Clear contents save the delete / move / new row.

Same with columns but these are ever slower, chink …. Chink regardless or power VBA codes. Just that way.

The reason is excl need to address and re address that sheets data and check it correct each time the standard square (65536x256 falls out of that square and will make sure its replaced.

EG if you delete row so that they are gone not seen or usable EXCEL still carries then, and has them there just out of reach – excel lives in a box, I guess is the simplest way to explain this

Turn of screen update will spin code faster un less you have doc changes and doc saves on the fly where S U D must be on.. is on needs..




__________________
Free Excel based Web Toolbar available here.

Jack in the UK
J & R Excel Solutions
"making Excel work for you"
Jack in the UK is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 04:00 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes