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 7th, 2002, 02:42 PM   #1
Albert Wijn
New Member
 
Join Date: May 2002
Posts: 2
Default

dear reader

Could you please help me,
I have following problem;

In Workbook "A" sheet1 cell B3 has a "name"

In Workbook "B" sheet1 contains in column 2 several cells corresponding "name"

I am trying already for a longer time now to create a macro with the result that all data in Workbook "B" column 1,3,5 are copied to Workbook "A".

So there should be a search function for "name" out of Workbook "A", and that find in column 2 from Workbook "B" all the corresponding cells and than copy the data from column 1,3,5 of these corresponding cell's to Workbook "A"

hope this explains my problem, could you help me?

many thanks
Albert
Albert Wijn is offline   Reply With Quote
Old May 7th, 2002, 04:23 PM   #2
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

This worked for me.

Workbook A, sheet1 is the originating sheet.
Workbook A, sheet2 is the final destination sheet.
Workbook B, sheet1 is the data sheet, where the values are found and copied.

Code:
Sub test()
Dim Wkbk1 As Workbook, Wkbk2 As Workbook
Dim origSheet As Worksheet, destSheet As Worksheet
Dim findSheet As Worksheet
Dim SourceCell
Dim lastrow1 As Long, x As Long, lastrow2 As Long
Dim acopy, ccopy, ecopy

Set Wkbk1 = Workbooks("A.xls")
Set Wkbk2 = Workbooks("B.xls")

Set origSheet = Wkbk1.Sheets("Sheet1")
Set destSheet = Wkbk1.Sheets("Sheet2")

Set findSheet = Wkbk2.Sheets("Sheet1")

With Wkbk1
With origSheet
    SourceCell = .Range("B3")
End With
End With

With Wkbk2
With findSheet
lastrow1 = .Cells(Rows.Count, "B").End(xlUp).Row
For x = 1 To lastrow1
    If .Cells(x, 2) = SourceCell Then
        With Wkbk1
        With destSheet
            lastrow2 = .Cells(Rows.Count, "A").End(xlUp).Row
                With Wkbk2
                With findSheet
                    acopy = .Cells(x, 1).Value
                    ccopy = .Cells(x, 3).Value
                    ecopy = .Cells(x, 5).Value
                End With
                End With
            .Cells(lastrow2 + 1, 1) = acopy
            .Cells(lastrow2 + 1, 3) = ccopy
            .Cells(lastrow2 + 1, 5) = ecopy
        End With
        End With
    
    End If
       
Next x
End With
End With
MsgBox "Done!"
End Sub
It copies columns 1,3,5 of any found value in column 2 to A.xls sheet2, columns 1,3,and 5 respectively.

Let us know how this works out. If you want the entries to go elsewhere, please be specific and I or somebody else will adjust them

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 7th, 2002, 04:31 PM   #3
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Nice piece of code Jay. The "with"'s gave me a headache though.


__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old May 7th, 2002, 04:39 PM   #4
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-05-07 15:31, Mark O'Brien wrote:
Nice piece of code Jay. The "with"'s gave me a headache though.


Thanks! No kidding on the headache part. I can't see straight right now.

I tried to do this without selecting anything and the references crushed me. There has got to be an easier way to do this and not use .select or .activate, but this should be pretty fast.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 7th, 2002, 06:11 PM   #5
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

A different way, out of the loop (sort of...). I created a third sheet for the destination, it seemed like the source sheet already has data on it (i.e, b3 has the search term, etc....).

Code:
Sub test2()
Dim Wkbk1 As Workbook, Wkbk2 As Workbook
Dim origSheet As Worksheet, destSheet As Worksheet, srcSheet As Worksheet
Dim SourceCell, myrw As Long, myrw2 As Long
Dim mycl As Range, mycl2 As Range

Set Wkbk1 = Application.Workbooks("A")
Set Wkbk2 = Application.Workbooks("B")
Set srcSheet = Wkbk1.Sheets(1)
Set origSheet = Wkbk2.Sheets(1)
Set destSheet = Wkbk1.Sheets(2)

SourceCell = srcSheet.[b3].Value
Set mycl = origSheet.Columns("b").Find(What:=SourceCell)
If mycl Is Nothing Then GoTo 1:
myrw = mycl.Row
myrw2 = destSheet.[a65536].End(xlUp).Offset(1).Row
origSheet.Range("a" & myrw).Copy destSheet.Range("a" & myrw2)
origSheet.Range("c" & myrw).Copy destSheet.Range("c" & myrw2)
origSheet.Range("e" & myrw).Copy destSheet.Range("e" & myrw2)
again:
Set mycl2 = origSheet.Columns("b").Find(What:=SourceCell, After:=mycl)
If Not mycl2 Is Nothing Then
    If mycl2.Row > mycl.Row Then
        myrw = mycl2.Row
        myrw2 = destSheet.[a65536].End(xlUp).Offset(1).Row
        origSheet.Range("a" & myrw).Copy destSheet.Range("a" & myrw2)
        origSheet.Range("c" & myrw).Copy destSheet.Range("c" & myrw2)
        origSheet.Range("e" & myrw).Copy destSheet.Range("e" & myrw2)
        Set mycl = mycl2
        GoTo again
    End If
End If
MsgBox "Done!"
End
1: MsgBox "Could not find any cells matching cell B3."
End Sub
Have a good one y'all.

_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-07 17:16 ]
NateO is offline   Reply With Quote
Old May 8th, 2002, 12:10 AM   #6
Albert Wijn
New Member
 
Join Date: May 2002
Posts: 2
Default

Jay

Many thanks, this is perfect. I should have asked it earlier would have safed a lot of time.

thanks, thanks

Albert
Albert Wijn 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:49 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