+ Reply to Thread
Results 1 to 7 of 7

Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy.

  1. #1
    Registered User
    Join Date
    09-22-2014
    Location
    ZA
    MS-Off Ver
    2016 or 365
    Posts
    12

    Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy.

    Hi guys and excel VBA experts.
    I need a bit of help here.
    I have been successful in adapting code to my needs and did ask for a lot of help in a previous post. It's done in a messey way, but works. I would like to optimise the code once this part is resolved and would appreciate someone looking at my code and optimizing it if they are willing.
    However, in this request, I need help with a small portion of the code as I have been able to get it to work if my source is in a specific order, which is not always the case.

    So here goes.
    The hidden sheet called "Temp" holds data that is pasted from the Windows clipboard. It is all in format Text, just as I need it.
    There is another sheet called "Analyse" where only certain columns are copied from the "Temp" sheet.

    They are in a defined array ar with the following column names "CRN", "Customer Name", "Circuit/Equip ID", "Extension", "SLA", "Service Type", "Status".
    These columns are not allways in the correct order as above in the "Temp" sheet, and there are other colums inbetween these columns too.
    The destination columns however need to remain in the order as indicated in the array. I have measures and lookup values in the destination sheet.
    A third sheet holds pivot tables that uses the "Analyse" sheet table for some simple measures.

    What I need to happen is that when Analyse is run, the desired columns need to be "looked for" in sheet "Temp", copied from row 2 down, and pasted in the defined order in the "Analyse" sheet from row 2 down.
    In the "Analyse" sheet there are currently formulas for INDEX and MATCH in columns H, I and J that are using three other sheets to lookup data and fill those cells, but I am working on a solution to add the formula using VBA on the cells, only if column D has a value as that one is "Customer Name" and will never be blank. Suppose I could also just count the rows of data and fill the formula from row 2 down to last data row, and use this for the three formulas.
    Also currently my code removes the headers in the "Analyse" sheet as I select the sheet columns A:J and use ClearContents, which then breaks the measures in the Pivot Tables as the table headers are missing. This I need to fix that it clears from row 2 down, but only once I resolve the formula entry with VBA.

    I have started on another project where I'm using enums, but I don't quite understand it yet and have other classes starting which is leaving me with little time to figure this request out. I know I will eventually, but I have about 60 people using this spreadsheet and I cannot go to every individual to rearange their desktop app's columns for the source. I would appreciate any help that is provided. Efficient code is a must as I need this to run as fast as possible, which it is not doing too bad at the moment.

    I've had to include compatability for older OS and Office versions. Oh and there's a search form that is used on one of the sheets too.

    Here follows my code.

    Please Login or Register  to view this content.
    Last edited by HandSM; 01-15-2020 at 03:29 AM.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy

    Hi there, good morning,
    I have quite a lot of imports and projects doing similar things.
    My suggestion is the following; add an extra worksheet (may be hidden too) where you list your target column's headers in column A and in column B the column they're in (this jus in case you decide to change that order too)
    In the columns after that you place the column names of the temp sheet next to the one you want it to be copied to.
    let's say Column A is CRM and Column B = 2 the the corresponding column tempsheet header Column C = fieldX

    Hav you routine lookup the tempsheets headet and reurn the number in B

    If you attach a sample file WITH your macro code and dummy data in target as well as tempsheet I'll try and exp,lain in there in a netter way.
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

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

    Re: Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy

    Please Login or Register  to view this content.
    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
    09-22-2014
    Location
    ZA
    MS-Off Ver
    2016 or 365
    Posts
    12

    Re: Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy

    @AlphaFrog
    This does exactly what my code does. Probably cleaner, but does not paste destination sheet columns in correct order.
    My columns names might change in time, as I may add specific one's.
    I could have in the Temp sheet Customer Name and then CRN and the rest which then places the copied columns in the wrong order, so the find in array does work if the columns are in the same order in Temp, just not if the order is different.
    I either need to copy column by column to place in the right order, or, say if column 1, 2 and 3 are in the right order, copy and paste, then look for 4, paste it to column 4 , look for the next in the array, say 5 and 6 are correct, copy and paste to destination sheet.
    @ Keebellah
    I have seen something of what you mention in your reply, but this is probably not what I need. As I say, the columns are better hard coded. I was thinking using enums to accomplish it, but I don't quite understand how they work yet.
    I am leaving the Temp sheet in place and clearing its full contents.
    The output sheet "Analyse" I am clearing rows 2 down to last row, columns A:G, I just can't clear H, I and J as I have INDEX and MATCH lookups in those columns currently to 2000 rows. I have been working on changing the formula's to be filled with VBA in those columns, but also having a little problems there.
    Do I update the original question code, with less detail?

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy

    Hi, if you can, please upload a sample file(s) with dummy data to reproduce the process. Will make it much easier to 'see' and also test it.
    Make sure you have the macro code included (macro embedded file) as well.

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

    Re: Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy

    Quote Originally Posted by HandSM View Post
    @AlphaFrog
    This does exactly what my code does. Probably cleaner, but does not paste destination sheet columns in correct order.
    This will paste in the order you have the headers in the array in the code.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-22-2014
    Location
    ZA
    MS-Off Ver
    2016 or 365
    Posts
    12

    Re: Copy selected columns from Sheet1 to Sheet2 VBA solution required. Not a straight copy

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    I found my mistake, while putting together a sample workbook. Thank you for the above. I now need to expand on it if the Temp sheet is empty and analyse is clicked. There is no more error handler working as it did before. So I will have to post two new requests I suppose. Thanks again.

+ 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. [SOLVED] Copy 2 Columns of Data from Sheet1 and Paste in Sheet2
    By salloush in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-20-2019, 09:19 PM
  2. [SOLVED] Copy selected data from sheet1 to sheet2
    By Rajesh shishodia in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-03-2015, 06:20 AM
  3. Replies: 0
    Last Post: 05-22-2014, 08:10 AM
  4. Replies: 6
    Last Post: 07-25-2013, 02:58 PM
  5. [SOLVED] Copy Selected Rows from Sheet1 to Sheet2
    By abjac in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-08-2012, 05:34 AM
  6. Copy sheet1 columns to sheet2 automatically
    By charlie.cale in forum Excel General
    Replies: 0
    Last Post: 11-11-2011, 04:20 PM
  7. How to copy some columns from sheet1 to sheet2 or sheet3
    By wlarson in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-30-2007, 08:58 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