+ Reply to Thread
Results 1 to 3 of 3

VBA to find unique code, copy whole row to new worksheet, do next until end of used rows?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-31-2015
    Location
    East Midlands
    MS-Off Ver
    Excel 2019
    Posts
    132

    VBA to find unique code, copy whole row to new worksheet, do next until end of used rows?

    Windows 10
    Excel 2019


    I have searched extensively online without understanding much of what is returned and
    I have searched this forum but all posts on here are empty of any text for some treason.

    What I am trying to achieve is that the VBA code looks at the "Stock Inventory" worksheet, cycles or looks at each row from what would be Column A to G, and returns the first unique row based on the Unique Code in Column G.

    So in the example below, from the "Stock Inventory" worksheet it would copy, Rows 8, 10, 15 and 18 into the "Product Summary" worksheet.


    Column A = Product Base
    Column B = Product type
    Column C = Pack type
    Column D = Pack Size
    Column E = Supplier
    Column F = Unique Code (this could be a all numbers, all letters or mix of both)
    Column G = Stock Level
    Column H = Reorder

    Due to various reasons, there will be duplicate entries of the same product with the same Unique Code.

    Using VBA, I need it to look at each row in the worksheet ?Stock Inventory?,
    find the first occurance of a block of unique code in Column F,
    select that row from Column A to Column F,
    and copy the data into the appropiate "pre-defined" columns in the worksheet "Product Summary"
    move on to the next block of unique code.
    continue until the end of used cells

    I am unable to work out how to achieve this.

    If anyone is able to help I have attached an example workbook.

    Thanks in advance
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,981

    Re: VBA to find unique code, copy whole row to new worksheet, do next until end of used ro

    You could just copy the whole range to the Product Summary and then use the Remove Duplicates functionality.

    See: https://support.microsoft.com/en-us/...e-66a552dc138d
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA to find unique code, copy whole row to new worksheet, do next until end of used ro

    Hello. You have two basic ways to do it:

    a) Copy the entire data range to another sheet and there apply the 'Remove Duplicates' method (it's what @TMS told you), or

    b) Apply an Advanced Filter and do the same in one step: I show you this variant.

    Sub Macro8()
    Dim Rng As Range
    Set Rng = Range("'Stock Inventory'!A1").CurrentRegion.Resize(, 6)
    With Sheets("Product Summary")
      .Cells(1).CurrentRegion.Delete xlShiftUp
      .Cells(2, 1) = "=CountIf(" & "'" & Rng.Worksheet.Name & "'!F$2:F2, '" & _
        Rng.Worksheet.Name & "'!F2) = 1"
      Rng.AdvancedFilter 2, .Range("A1:A2"), .Range("A3"), False
      .Rows("1:2").Resize(, 6).Delete xlShiftUp
    End With
    End Sub
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

+ 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. Correct Macro to find Unique Item in List & Copy Record to New Worksheet
    By Wanttobeexcelpro in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2022, 09:30 PM
  2. [SOLVED] Find last cell in column and copy unique records to another location on the same worksheet
    By ExcelRonin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2016, 05:28 PM
  3. [SOLVED] How to copy rows to new worksheet based on unique first character in column
    By G-N-A in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2013, 12:43 PM
  4. How to copy rows to new worksheet based on unique first character in column
    By G-N-A in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2013, 09:52 AM
  5. How to copy rows to new worksheet based on unique first character in column
    By G-N-A in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-12-2013, 09:52 AM
  6. Code to find text, copy rows into new worksheet, based on relative selection
    By tribes712 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-21-2010, 11:32 AM
  7. ADD,NAME, and COPY UNIQUE ROWS FROM UNKNOWN WORKSHEET
    By GACHETT in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-07-2006, 09:20 AM

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