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 Apr 20th, 2002, 11:27 PM   #1
Sunrise
New Member
 
Join Date: Apr 2002
Posts: 5
Default

Every month I have to do a large data sorting exercise and I currently do it all by hand so I want to learn how to do it using macros. I have 3 columns of data: A, B and C. How can I search down the entire column A, row by row, looking for the equivalent value and/or string in column B? If column B does in fact have an equivalent value and/or string then move both the column B and column C cell values up to the same row as column A.

Example: Using the value in cell A1 (e.g. value is 25000), search column B for the value 25,000. If the value is found in column B (e.g. say on row 200) then move (by Cut & Paste) both the column B and the column C cells (both also on row 200) up to cells B1 and C1 (i.e. so they are level with the A1 value). If 2 occurrences of the 25,000 value are found (note: there can be more) in column B then for each additional value found add a blank row before Cutting & Pasting the additional value into cells B2/C2, or B3/C3 etc. etc. Once done, repeat the same process for the next value in the original column A.

That’s essentially the process I now go through manually for worksheet data that is 800-900 rows long. It takes me quite a few hours to finish it and I would like to have a macro that can do it all much more quickly. Can anyone help me? I'm sure it is fairly complex answer so if you cannot I thank you in advance anyway.
Sunrise is offline   Reply With Quote
Old Apr 21st, 2002, 07:43 AM   #2
smozgur
BatCoder
 
Join Date: Feb 2002
Location: Turkey
Posts: 764
Default

Hi Sunrise,

This code does what you need.
Assume first row is caption row (no data)
and if there is not corresponding value for any A column value then that row would be blank.

Hope this works for you.

'///CODE START///
Sub JustDoIt()
Dim i As Integer, j As Integer, k As Integer
Dim p As Integer, L As Integer
Dim A(), BC()
Range("A1").Select
Selection.End(xlDown).Select
L = Selection.Row - 1
ReDim A(L, 2)
i = 2
Do
p = p + 1
A(p, 1) = Cells(i, 1)
j = -1
For k = 2 To L
If Cells(i, 1) = Cells(k, 2) Then
j = j + 1
End If
Next k
A(p, 2) = j + 1
k = 1
For k = 1 To j
Rows(i + k & ":" & i + k).Select
Selection.Insert Shift:=xlDown
Next k
L = L + k
i = i + k
Loop Until Cells(i, 1) = ""
ReDim BC(i - 1, 2)
L = i - 1
For i = 1 To L - 1
BC(i, 1) = Cells(i + 1, 2)
BC(i, 2) = Cells(i + 1, 3)
Next i
k = 2
For i = 1 To UBound(A, 1)
If A(i, 2) = 0 Then
Cells(k, 2) = ""
Cells(k, 3) = ""
k = k + 1
Else
For j = 1 To L - 1
If A(i, 1) = BC(j, 1) Then
Cells(k, 2) = BC(j, 1)
Cells(k, 3) = BC(j, 2)
k = k + 1
End If
Next j
End If
Next i
End Sub
'///CODE END///

Regards





_________________
Oz ~ TheWordExpert

[ This Message was edited by: smozgur on 2002-04-21 06:44 ]
smozgur is offline   Reply With Quote
Old Apr 21st, 2002, 11:14 AM   #3
Sunrise
New Member
 
Join Date: Apr 2002
Posts: 5
Default

Thank you very much for your help. There seems to be some overwriting of the test data that I just tried it with. I think I will try and work on the code you provided so I can try and learn something about macros and how VBA works.

I appreciate your help - it is invaluable. I know you put in a good deal of time to write this for me and perhaps I can walk through and tweak it further to make it work perfectly for me. If I cannot then rest assured I will post something about my troubles.
Sunrise is offline   Reply With Quote
Old Apr 21st, 2002, 02:22 PM   #4
smozgur
BatCoder
 
Join Date: Feb 2002
Location: Turkey
Posts: 764
Default

It's my pleasure Sunrise.

If you have trouble about it please let me know via email.

Edited:
I am completely sure that it was working correct with 99 data cell. Can you provide an excel file to test for me?


_________________
Oz ~ TheWordExpert

[ This Message was edited by: smozgur on 2002-04-21 13:24 ]
smozgur 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:16 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