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, 09:40 AM   #1
thexfactor44
New Member
 
Join Date: Feb 2002
Posts: 26
Default

I have code that compares a value on two sheets and if they are redundant, deletes the entire row on one sheet. I need to be able to incriment through all the cells in column A on Sheet 1 though, not like it is now, hard-coded to A1.
logically, i need this:
if "sheet2:cell An" (where 'n' is any cell in sheet 2:column A) is equal to "sheet1:cell An", then delete row on sheet1 where value is redundant.

Do i need a for loop? I have no idea. this is the current code i have come up with:

Sub X()

Sheets("Sheet2").Select
sheet2 = Range("A1")
Sheets("Sheet1").Select
sheet1 = Range("A1")
If sheet2 = sheet1 Then
Rows("1:1").Select
Selection.Delete Shift:=x1up
End If

End Sub

Thanks to all that can help!
thexfactor44 is offline   Reply With Quote
Old May 24th, 2002, 09:58 AM   #2
kkknie
Board Regular
 
Join Date: Apr 2002
Location: Greenwood, SC
Posts: 677
Default

Here you go...


Sub DeleteRedundants()

Dim i As Integer, j As Integer

'Loop through first sheet
For i = 1 To 10000

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

'Loop through second sheet
For j = 1 To 1000
'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



Hope this helps,

K

[ This Message was edited by: kkknie on 2002-05-24 08:58 ]
kkknie is offline   Reply With Quote
Old May 24th, 2002, 10:58 AM   #3
thexfactor44
New Member
 
Join Date: Feb 2002
Posts: 26
Default

thanks for the code. it works great. I have one issue though (due to my lack of explanation):

the two columns being compared are about 25000 cells deep. I realize I need to change your code on the range section to accomidate. The problem is that this method is too hard on memory and excel locks up prtty bad...for about 30 minutes. is there a way to go about this using less memory?

If there is another method and you exit on blank cells, couldn't the range (insidde the for;to) be set from 1 to 65000? thanks.

I appreciate your help.
thexfactor44 is offline   Reply With Quote
Old May 24th, 2002, 11:20 AM   #4
Joe Was
MrExcel MVP
 
Joe Was's Avatar
 
Join Date: Feb 2002
Location: Central Florida, USA
Posts: 7,541
Default

I posted this code today for another, you may be able to exchange your test for the one in the code below. This code is fast!

This "Sheet Module" code will work from a hot-key or Form Button. For any value in Column "C" that is value less than 10 (<10) the entire row is deleted. Hope this helps. JSW

Sub DRow()
'Find all the rows for which column "C" < 10 and delete it.

Application.ScreenUpdating = False
Worksheets("Sheet1").Select
Range("C1").Select
n = 1
n = n + 1
For Each r In Worksheets("Sheet1").UsedRange.Rows
n = r.Row
If Worksheets("Sheet1").Cells(n, 3) < 10 Then
Worksheets("Sheet1").Cells(n, 3).Select
Selection.EntireRow.Delete
End If
Next r
Application.ScreenUpdating = True
'Range("A1").Select
End Sub


[ This Message was edited by: Joe Was on 2002-05-24 10:23 ]
Joe Was is offline   Reply With Quote
Old May 24th, 2002, 11:26 AM   #5
thexfactor44
New Member
 
Join Date: Feb 2002
Posts: 26
Default

I wish I knew how to port the code to comething I could use, but I dont know VB at all..I am just playing with the methods I understand from scripting languages like Perl. It looks like you have the method right though, by switching variables into memory 1 at a time..this is what I would like to do, but i really dont know how. If this is easy, I would really appreciate help. If it is not, I will plug away and not waste all your time and try myself..as I would like to learn this stuff...just not when deadline is pushing. Thanks for your help though.
thexfactor44 is offline   Reply With Quote
Old May 24th, 2002, 12:12 PM   #6
thexfactor44
New Member
 
Join Date: Feb 2002
Posts: 26
Default

I cannot do it from what i know. this is sorta a final cry for help...
thexfactor44 is offline   Reply With Quote
Old May 24th, 2002, 03:57 PM   #7
Joe Was
MrExcel MVP
 
Joe Was's Avatar
 
Join Date: Feb 2002
Location: Central Florida, USA
Posts: 7,541
Default

Try this. Once the row addresses change due to a delete this code will ignore any duplicates who's row address does not match!

So after the first pass your two sheets will not match. Any duplicates not on the same row on both sheet will not be deleted.

If you want all duplicates no matter what row they are in deleted we can approach the task with a different macro. If a different macro is what you need repost and I will try to build you one. JSW


Sub DSRow()
'Find all the rows for which column "A" on Sheet1 = the same row & column on
'Sheet2 and delete that row from sheet1.
Application.ScreenUpdating = False
Worksheets("Sheet1").Select
Range("A1").Select
n = 1
n = n + 1
For Each r In Worksheets("Sheet1").UsedRange.Rows
n = r.Row
If Worksheets("Sheet1").Cells(n, 1) = Worksheets("Sheet2").Cells(n, 1) Then
Worksheets("Sheet1").Cells(n, 1).Select
Selection.EntireRow.Delete
End If
Next r
Application.ScreenUpdating = True
'Range("A1").Select
End Sub
Joe Was is offline   Reply With Quote
Old May 24th, 2002, 04:05 PM   #8
thexfactor44
New Member
 
Join Date: Feb 2002
Posts: 26
Default

Wow! we're close i think. I need to be able to find if ANY value in cloumn A on sheet 1 = any value in column A on sheet 2...not row dependent...and if there is a match, delete that row from sheet 2.
so ie.:
sheet 1 a14="hello world"
sheet 2 a31412="hello world"

delete row 31412 on sheet 2.

thanks so much..i can tell we're close here...
thexfactor44 is offline   Reply With Quote
Old May 24th, 2002, 04:11 PM   #9
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

I responded to your other thread.

Please don't start a new thread on the same topic. It makes it hard to follow what has already been requested/suggested, etc. Just continue with a follow-up in the same thread.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 24th, 2002, 04:26 PM   #10
Joe Was
MrExcel MVP
 
Joe Was's Avatar
 
Join Date: Feb 2002
Location: Central Florida, USA
Posts: 7,541
Default

I tested Jay's code and if I understand your desire, his code will work!

I am re-posting Jay's code here for you. Note: I did not see your other posts until Jay pointed it out. On your original post if you Reply back to your self adding more information about your question, your question will go to the top of the list and everyone will see it on the first page. Do not open a new question about a question you have on the board. It makes it hard for us answering. JSW

Sub Dtest()
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
Joe Was 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 11:11 AM.


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