+ Reply to Thread
Results 1 to 10 of 10

Macro to copy neighboring columns based on value of a reference cell

  1. #1
    Registered User
    Join Date
    04-16-2014
    Location
    vancouver
    MS-Off Ver
    Excel 2007
    Posts
    18

    Macro to copy neighboring columns based on value of a reference cell

    Hi,

    First I'd like to thank everyone who contributes on this forum. I have learned an amazing amount of information about excel just from searching other threads and am constantly impressed with the speed and quality of the responses. I hope that I can get some help with a project that I'm working on.

    I'm trying to create a macro to copy the data from 2 columns (B and C) based on the value in A.

    I've attached a sheet with what I've got so far. There is macro already to pull the unique values and then another to insert a couple blank lines between each row. I can't figure out how to then copy and paste special-transpose the values depending on the value in Column A. I hope that makes sense... The reason for the change in formatting is to allow the data to be copied into a separate sheet which automatically puts it into charts for comparison.

    The first few rows of Sheet3 show what I hope the eventual output will look like.

    Thanks in advance and if anything is unclear, please let me know.

    Graham
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Macro to copy neighboring columns based on value of a reference cell

    I have written an example in attached file.

    I have assumed that range is already sorted on column A.
    Result is stored in Sheet4.

    Hope this can help you.

    \Joakim
    Attached Files Attached Files
    1. Reply to thread and inform if suggestion was helpful or not
    2. Click on the star (=Add Reputation) if you think someone helped you
    3. Mark [SOLVED] to this thread if solution was found. (On Menu "Thread Tools" > "Mark this thread as solved")

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to copy neighboring columns based on value of a reference cell

    Here's another method for what it's worth even though Joakim N's code is nice as well.

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 04-30-2014 at 09:48 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    04-16-2014
    Location
    vancouver
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Macro to copy neighboring columns based on value of a reference cell

    Thanks to both Joakim and AlphaFrog for these solutions. They both work very well and have saved me a lot of time.

    Is it possible to make an adjustment to the macro to allow the original data to be filtered? The data would be filtered based on the values in column A and the output would need to show the results from only the filtered cells.

    Thanks!

  5. #5
    Registered User
    Join Date
    04-16-2014
    Location
    vancouver
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Macro to copy neighboring columns based on value of a reference cell

    I'm still hoping to find a way to copy only the filtered cells if anyone has any thoughts... Thanks

  6. #6
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Macro to copy neighboring columns based on value of a reference cell

    I have added code that only contemplate range after autofilter is applied. I hope it works.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    04-16-2014
    Location
    vancouver
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Macro to copy neighboring columns based on value of a reference cell

    Thanks, Joakim! That works perfectly! Out of curiosity, why did you use the xlpinyin sort method? Isn't that just for sorting Chinese characters?

  8. #8
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Macro to copy neighboring columns based on value of a reference cell

    Quote Originally Posted by excel151515 View Post
    Thanks, Joakim! That works perfectly! Out of curiosity, why did you use the xlpinyin sort method? Isn't that just for sorting Chinese characters?
    I recorded the code for sorting the range. Code xlpinyin was not removed but it probably could have been.

  9. #9
    Registered User
    Join Date
    04-16-2014
    Location
    vancouver
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Macro to copy neighboring columns based on value of a reference cell

    This one just keeps getting more and more complicated...I've been staring at that macro code for hours and can't figure out how to add an additional feature. I really hope you can help.

    I'm trying to copy a total of 4 columns onto the new sheet. Two of them will remain vertical and the other two will be horizontal. I've uploaded an updated sheet with the data on "Sheet 1" and the hoped for output on "Output". I've added colors to make it easier to follow, but they will not be in the actual sheet. The sheet will need to remain filterable.

    I really appreciate the help with this project!
    Attached Files Attached Files
    Last edited by excel151515; 05-12-2014 at 04:45 PM. Reason: The spreadsheet was not correct

  10. #10
    Registered User
    Join Date
    04-16-2014
    Location
    vancouver
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Macro to copy neighboring columns based on value of a reference cell

    Figured it out

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 04-04-2014, 11:34 AM
  2. [SOLVED] Returning value from one column based on two neighboring columns
    By Ezzard in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-26-2014, 11:50 AM
  3. Macro to copy multiple columns to different sheet based on cell value
    By rexer231 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2013, 11:37 AM
  4. Sum multiple columns based on a reference cell
    By nielsen555 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-31-2013, 04:42 PM
  5. Sum columns based on cell reference.
    By danroll in forum Excel General
    Replies: 2
    Last Post: 01-20-2010, 07:47 AM

Tags for this Thread

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