+ Reply to Thread
Results 1 to 9 of 9

Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

  1. #1
    Registered User
    Join Date
    01-17-2012
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    Hi all,

    Back for some friendly advice after receiving some very good and prompt advice the last time I posted!

    I have a sheet containing data for 3 products, each one includes a graph.

    I'd like a macro which copies all of the data (including the graph) from the sheet containing the data (sheet1) to a summary sheet (sheet2) based on the selection from a validation cell in sheet2.

    For example; the user goes to sheet2, selects which product data they wish to see using the drop down validation cell, clicks the macro button and the corresponding data (including graph) is copied from sheet1 into the space below on sheet2.

    I hope i've explained that well enough; I'm new to all this so was fiddling around with IF functions and tried to pull it into VBA but got way out of my comfort zone. Hopefully you can help .

    ExampleSheet.xls
    Last edited by drgwhizz; 01-25-2012 at 10:49 AM.

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    hi drgwhizz, please check attachment
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    01-17-2012
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    Thanks watersev,

    The code works great for the example sheet. I have adjusted the layout slightly on sheet1 which means that the terms "product 1", "product 2", "product 3" now fall above and below the product data tables as well as heading each table. This means that the macro misses the table out when searching for the matching terms.

    Is there a way to adjust the code to search from the first column along (i.e. left to right) rather than top to bottom (which it appears to be doing now). I've tried to tweak the code but my limited knowledge is making the task that bit harder!

    I presume this is the line which needs adjusting but don't know what needs tweaking :
    Please Login or Register  to view this content.
    I've attached a copy of the spreadsheet so you can see my explaination a bit better. One table will sit above the product tables and one below; each with the product names in them. This appears to be causing a problem with the macro.

    ExampleSheet(11).xls

    Thanks in advance.

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    can you post exact layout of the sheet1 data tables and charts? I'm not sure I quite understand additional info provided on the sheet.

  5. #5
    Registered User
    Join Date
    01-17-2012
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    ExampleSheet(12).xls

    Apologies for the poor explaination; I know it would be easier to post the workbook I'm working within but it's too large and cumbersome for posting.

    I've adjusted the example sheet to try and make it more clear. The table at the top of sheet1 (table 1) is getting in the way of the macro procedure. The product names within table 1 are picked up and copied rather than the product data tables (the ones with the graphs).

    I hope that makes sense?!

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-17-2012
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    Thanks again Watersev that's fixed the issue I created by changing the layout .

    Just one last side question. If I decide to put formulas in the product data tables the macro will copy the formula cell references across to the new sheet. For example, if I place a formula which relates the column D in the product data tables to data in table 1. The outcome on the copied version (sheet2) will reference the same cells but on sheet2 rather than sheet1; this obviously will change the values in the copied table.

    Is there a way to adjust this? Maintaining the references in the copied table back to the original data in sheet1?

    Again I apologise if my explaination is poor. I've attached an updated example.

    ExampleSheet(13).xls

    Thanks for your help so far + rep for your patience.

  8. #8
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    the simplest way is to take that into account while inserting formulas: if the data is located on Sheet1 and will not be transfered over to Sheet2, fix it to Sheet1, example:

    Please Login or Register  to view this content.
    The easiest VB option is to make a link of all numbers to Sheet1. Though it will not preserve formula but the "linked" value from Sheet1 (may be it will suit your needs):

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-17-2012
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2

    Doh! Didn't think to change the cell references to include the sheet name also. I've done this as it helped me keep things a bit simpler on the VB front.

    Thanks for the help, really appreciate it.

    I'll mark this thread 'Solved'.

+ 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