+ Reply to Thread
Results 1 to 16 of 16

Copy multiple cells based on another cell's value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Copy multiple cells based on another cell's value

    Hello!

    I need help with multiple linked values and their sorting, copying..

    I have one workbook with values like this (informative only)
    A | B | C | D | E | F | G
    ID No. | Height | Weight | Age | Color | Rating | Price

    ID number is the most important value, based on that value all other values are entered manually and that's ok. Id number grows as rows add (row 2= ID 252, row 3 = ID 253, etc.).

    I need to copy those values to another workbook but in different order. ID numbers are already in the other workbook, I just need to copy other values connected to ID number in order like this:

    A | B | C | D | E | F | G
    ID No. | Rating | Price | Color | Weight | Height | Age

    So basically I'm constantly adding, copying values to Workbook 2 manually..
    Can it be done more time saving and how?

    I was thinking of a macro button next to each ID Number which will copy values in corresponding columns into the other woorkbook in different order. Or, can that be automatic? With one button press?
    I really don't know how to do that. I want to get rid of manually copying of every value to another workbook, so I need advice..

    Also, since those workbooks have large amount of data, would it be better to store them in access. I'm pretty newbie to this so any advice, guideance will be helpful..

    Thanks in advance!

    Vedran C.
    Last edited by vedranc; 10-16-2010 at 02:04 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    You can look up data from a "database" (your main workbook) from your other workbook with simple lookup formulas. As long as the matching ID is in column A, you can use a VLOOKUP().

    In workbook 2 to get the RATING for the ID listed in cell A2 on that sheet, put this formula in B2:

    =VLOOKUP($A2, '[Book1.xls]Sheet1'!$A:$F, 5, 0)

    Just change the red number for the column you want to return a value from.

    Height = 2
    Weight = 3
    ...etc.

    Of course you'll need to edit the workbook name.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Copy multiple cells based on another cell's value

    Thank you for a swift response!

    It works!

    Only few more questions:

    1. Can cells which still have no value (#N/A) be somewhat different from other cells and without that text #N/A. It would be nicer to look at, especially if they would be only light coloured..

    2. Also, once cell looks-up for a source value, can it be turned into text/number/combination, so that it wouldn't be a formula anymore? Once cells have values they don't need to look it up anymore.

    3. Since there's a lot of information, lots of data (until now 37055 rows, 21 columns), would it be safer, easier to store that database in access format and manipulate with it in likewise manner? If yes, please recommend me where to start with reading

    Those databases are generated manually, entering one value after another, so maybe in Access it could be automated somehow?

    Thanks again!

    Vedran C.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    1) You can use Conditional Formatting to change the appearance of the cells with #N/A in them, perhaps change the color of the font so that it's invisible?

    2) You can remove formulas anytime you want by:

    a) highlighting a range of cells
    b) Ctrl-C to copy them
    c) Click on Edit > Paste Special > Values

    3) Access is a better database, of course. You would have to pose Access questions to Access forums/users.

  5. #5
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Copy multiple cells based on another cell's value

    Thank you JBeaucaire!

    I used Cond.Formatting to hide #N/A..

    Another question:

    I modified my database, added another column which is now the first column. So vlookup works with cells in B column.
    That first column represents let's say a city, so one more category. For now it will be entered manually. What I want is to separate rows with identical City value, and copy them to another workbook, one after another, like in original workbook, but without empty rows where was different city value. Something like a separate collection of data corresponding to each City value.
    This kind of report would be done once in a while.

    How can I manage that?

    Vedran C.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    'SHEET1 TO MANY WORKBOOKS

    I have a macro that may be "ready to use" for parsing rows of data from one sheet to many workbooks named for the same values in that evaluation column.My macro names the workbooks for values in the column PLUS today's date, you can take a stab at removing the date part...or leave it in, it's a good technique.


    There are many various macros on the site for parsing data several ways, the code is meant to teach since often you need parts of one macro and parts of another to cover your specific needs.

  7. #7
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Copy multiple cells based on another cell's value

    It doesn't work right for me..

    Everything is being copied to another workbook, but with name of one city.. And it takes a lot of time. Might be because of large amount of data?

    On the end there's an error RT 1004, MOE cannot access the file 'C\2010'..
    When i pick Debug, this row is highlighted:

    ActiveWorkbook.SaveAs SvPath & MyArr(Itm) & Format(Date, " MM-DD-YY"), xlNormal

    What could be the problem?

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    The Variable names I use are meant to guide you. SvPath = Save Path...the folder on your hard drive where the files will go.

    Edit that code highlighted in red so that it points to your own proper save path, or create a 2010 folder on your C:\ drive.

  9. #9
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Copy multiple cells based on another cell's value

    Got it now!

    Strange thing..while I was trying to get the macro working, first row-header had sort buttons (like macro sort buttons)..now it's gone. How do I get them again and can they stay there? It helps a lot when I can only via button on header select which City value to show. Also, I noticed that when I sortone column, all others follow correspondingly..

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    My macro doesn't take into account any sort of buttons or whatnot. I can't comment on that blindly.

  11. #11
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Copy multiple cells based on another cell's value

    My mistake..When there was errorr (invalid save path), and I pressed End rather than Debug, my database was formed as a table, so Table Sort Buttons appeared. And it is just what i wanted to have,,

    At first Macro didn't work well, don't know why. The first workbook that was extracted had all City values in it, just like a copy of existing one. Other workbooks had each their city values. But, it was probably my fault.. Now I tried it again and it works great! Thank you so much!

    But, something else bothers me

    Since those workbooks have vlookups in it, they also transfer to separate workbooks, and so they are very large and seek connection with other workbooks. Can that macro be modified so that when database is extracted with respect to defined City value all formulas are erased in that new workbook leaving only values in cells?

    So when it creates new workbook, it also copies entire workbook or just that region filled with data and pastes it only as values. Is it possible? As I can see, region that is being copied from the original workbook stays selected, so there might be a way to add that copy-paste values command?

    Vedran C.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    Not knowing what changes you've made, I'll reference the original code. If you look at the copy / paste commands you could probably spot the line that is pasting ALL instead of pasting VALUES.

    'Loop through list one value at a time
        For Itm = 1 To UBound(MyArr)
            ws.Range(vTitles).AutoFilter Field:=vCol, Criteria1:=MyArr(Itm)
            
            ws.Range("A1:A" & LR).EntireRow.Copy
            Workbooks.Add
            Range("A1").PasteSpecial xlPasteValues
            Range("A1").PasteSpecial xlPasteFormats
            Cells.Columns.AutoFit
            MyCount = MyCount + Range("A" & Rows.Count).End(xlUp).Row - 1
            
            ActiveWorkbook.SaveAs SvPath & MyArr(Itm) & Format(Date, " MM-DD-YY"), xlNormal
            ActiveWorkbook.Close False
            
            ws.Range(vTitles).AutoFilter Field:=vCol
        Next Itm

    See it? I added another line directly below it to also paste in the cell formatting so that the appearance stays the same, too.

  13. #13
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Copy multiple cells based on another cell's value

    Great!

    You've helped me a lot! Saving appearance is also what I was looking for!

    Now I'll check the code in detail, to learn some more...

    Thank you!

    Vedran C.

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  15. #15
    Registered User
    Join Date
    10-12-2010
    Location
    Cakovec, Croatia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Copy multiple cells based on another cell's value

    Hello JBeaucaire!

    Something else bothers me. Seems like on some PCs won't save file under C: and other directories.. Can it be modified to save automaticly in a new folder (can it create a folder?), under certain name? Or in the same folder with original workbook. If not, then ok..

    And another one, how can I add another macro, separate from this one, which saves current copy of the entire original workbook, with only values pasted and formats, but with date in the filename? And to be asked where to save it..

    I noticed thath this macro doesn't work on excel 2000, stops somewhere..And also, on not so powerful computer, or with low memory available, it only generates exactly the same workbooks(doesn't extract according to column value) but with names according to Column values. Strange. Seems like I have to run the macro few times before it works right. That only happens on low memory computers (since cca13000 rows have data..

    Is it true that I could use this workbook only until cca16000th row? I've read that somewhere, but don't know if it applies to number of rows with lookups in it or until that absolute number?

  16. #16
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy multiple cells based on another cell's value

    1) For your other macro requests, not actually related to this thread, start a new thread.

    2) For your desire to save in a different path, just edit the SvPath to where you DO want to save to, my "C:\2010\" was just meant to get you started.

    3) I can't speak to Excel 2000 limitations, but I know of no reason the macro should stop halfway for any reason. That doesn't mean I don't believe you, just I can't help with earlier versions of Excel.

    Nothing in the macro is particularly memory intense. It does most of the work directly on the worksheets themselves, so I know of no reasons low memory would effect it either.

    4) You can use the link my signature to get my contact address from my website. I would be happy to look at the file you're actually using this on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1