Consulting

Results 1 to 6 of 6

Thread: Solved: Copy Unique Row(s) From One Sheet to Another

  1. #1
    VBAX Regular jdubya's Avatar
    Joined
    Oct 2006
    Posts
    13
    Location

    Smile Solved: Copy Unique Row(s) From One Sheet to Another

    Hi,

    Just discovered this site recently, and I?m impressed with the amount of good information that can be found.

    I would like to perform a web query and import the latest share prices of ten funds into a worksheet (Thrift Savings Plan - 401K plan for government workers).

    I?m thinking that the best approach would be to set up two worksheets: one for setting up the web query, and the other worksheet would keep the historical data, but I?m lost in how new share prices can be copied over and appended to the historical data sheet automatically without duplicates.

    Spreadsheet is attached (tspwebquery.xls).

    Program Version: Microsoft Excel 2003 (Version 11, Build 5612)

    I appreciate any help!

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Why not delete the old data and then copy the the new data to that sheet?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If you wish to keep the history you can delete duplicate rows based on the date column or column A using the following routine:
    [VBA]Sub DeleteDups()

    Dim x As Long
    Dim LastRow As Long

    LastRow = Range("A65536").End(xlUp).Row
    For x = LastRow To 1 Step -1
    If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
    Range("A" & x).EntireRow.Delete
    End If
    Next x

    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    this will copy the data to the data sheet and then delete all rows(in the data sheet) that have duplicate info in Col A except 1...
    [vba]
    Option Explicit
    Sub CopyAndRemoveDups()
    Dim cl As Range
    Dim ws As Worksheet
    Dim x As Long
    Dim LastRow As Long
    Dim rng As Range
    Dim rngToDelete As Range


    Set ws = Worksheets("query")
    With ws

    For Each cl In .Range("A2:A" & .Range("A65536").End(xlUp).Row)
    If cl.Value <> "" Then
    cl.EntireRow.Copy Worksheets("data").Range("A65536").End(xlUp).Offset(1, 0)
    End If
    Next cl
    End With

    Set ws = Worksheets("data")
    'Advanced Filter requires a header row - let's add a temporary one
    ws.Rows(1).Insert
    ws.Cells(1, 1).Value = "temp header"

    Set rng = ws.Range("A1:A10000")
    rng.AdvancedFilter xlFilterInPlace, unique:=True
    Set rngToDelete = rng.SpecialCells(xlCellTypeVisible)
    ws.ShowAllData
    rngToDelete.EntireRow.Hidden = True
    rng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    rngToDelete.EntireRow.Hidden = False

    'remove the temporary row
    ws.Rows(1).Delete
    End Sub[/vba]

    see attached....add data to the query sheet to see this work as all of the info you now have in the query sheet is a duplicate except the row 31-Oct-06.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Regular jdubya's Avatar
    Joined
    Oct 2006
    Posts
    13
    Location
    Quote Originally Posted by lucas
    this will copy the data to the data sheet and then delete all rows(in the data sheet) that have duplicate info in Col A except 1...
    [vba]
    Option Explicit
    Sub CopyAndRemoveDups()
    Dim cl As Range
    Dim ws As Worksheet
    Dim x As Long
    Dim LastRow As Long
    Dim rng As Range
    Dim rngToDelete As Range


    Set ws = Worksheets("query")
    With ws

    For Each cl In .Range("A2:A" & .Range("A65536").End(xlUp).Row)
    If cl.Value <> "" Then
    cl.EntireRow.Copy Worksheets("data").Range("A65536").End(xlUp).Offset(1, 0)
    End If
    Next cl
    End With

    Set ws = Worksheets("data")
    'Advanced Filter requires a header row - let's add a temporary one
    ws.Rows(1).Insert
    ws.Cells(1, 1).Value = "temp header"

    Set rng = ws.Range("A1:A10000")
    rng.AdvancedFilter xlFilterInPlace, unique:=True
    Set rngToDelete = rng.SpecialCells(xlCellTypeVisible)
    ws.ShowAllData
    rngToDelete.EntireRow.Hidden = True
    rng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    rngToDelete.EntireRow.Hidden = False

    'remove the temporary row
    ws.Rows(1).Delete
    End Sub[/vba]
    see attached....add data to the query sheet to see this work as all of the info you now have in the query sheet is a duplicate except the row 31-Oct-06.
    Steve,

    I've tried it, and it looks like this is what I want it to do!

    Thanks for your assistance!

    Jon


  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Glad to help Jon, be sure to mark your thread solved using the thread tools at the top of the page. If you have more questions be sure to post them. Just because you mark it solved doesn't mean its closed.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •