Closed Thread
Results 1 to 9 of 9

VBA Dynamic way to copy and paste unique values

  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    8

    VBA Dynamic way to copy and paste unique values

    Hi,

    I have a ever changing column of data (in no of rows). The starting point is given to C2. In this column I have the following values to pick a few:

    BB
    NT
    BB
    NO
    CB
    LE
    WO
    WO

    What I want to do is consolidate these values and paste under the existing values in column C (previously Ive used Selection.End(xlDown).Offset(1, 0).Select to find the end.

    My problems are the following:
    1. Consolidate does not work since it needs to specify my workbooks directory and sheet which will be changing from user to user and the sheet is created in the macro so it is not given a specific name.
    2. Advance filter copy Uniques does not work for some reason, I get duplicates trying to use this function (these duplicates does not appear when i manually try consolidation).

    As I said I can not specify a certain range however would work to choose entire C:D since rest is empty.

    Does anyone have a good idea how to solve this?

    Many thank you! I am sooooo stuck

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: VBA Dynamic way to copy and paste unique values

    I'm not for sure I understood your request.
    If I did, then:
    Backup your data.
    Copy the code below to a standard module (if you don't know how, ask).
    Run: Run_GetUniqueValues

    It was unclear to me whether you wanted a list of all unique values from both columns C & D (or just C).

    Thus in the code below you will see both:
    Please Login or Register  to view this content.
    For both C & D, remove the apostrophe and add it to the line above.

    The function: GetUniqueValues collects unique values from any range and stores it in a string separated by the vbCR character. Then Run_GetUniqueValues puts these values in a string array.

    You didn't mention if you wanted these values sorted. I added a Bubble sort function to sort these values.

    Then leaving an empty row, I copied these values below the previously last row of data in column C.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-12-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Dynamic way to copy and paste unique values

    Hi Steven,

    Thank you so much for your reply. Late last evening I actually made it work using the following:


    Please Login or Register  to view this content.
    It is definately not the most effective way but for my purpose it works fine

    Thank you!

    Joakim

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Dynamic way to copy and paste unique values

    This is a little more efficient:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-12-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: VBA Dynamic way to copy and paste unique values

    The problem with that kyle is that I dont want to remove the duplicates from the original data set, hence my copy into new area. I want to keep them in the original table just creating a "new set" with unique values so I can SUMIF from the above. (Yes it seems awkward to do it this way but the accounting system demands the format to be in this way)

    Joakim

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Dynamic way to copy and paste unique values

    No probs, just switch the code round a bit

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-15-2013
    Location
    St. Louis, MO
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: VBA Dynamic way to copy and paste unique values

    This is helpful. However, if there was a third column with different numeric values, how would I have it sum the values from that column in all of the duplicate rows into the consolidated one?

  8. #8
    Registered User
    Join Date
    05-12-2010
    Location
    Qatar
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: VBA Dynamic way to copy and paste unique values

    Dear StevenM
    Is is possible to sort the values based on alphanumerically by using your Function BubbleSortsArray(ByRef sArray() As String) fucntion.


    Thanks and regards,

    Sudhakar

    Your

  9. #9
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: VBA Dynamic way to copy and paste unique values

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

Closed 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