+ Reply to Thread
Results 1 to 7 of 7

Copy entry into a range if there is no duplication

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Copy entry into a range if there is no duplication

    Hi,
    I'm trying to create a "dynamic" db. Db is located in sheet1. Into sheet2, which has the same headers as sheet1, new monthly data will be thrown. Sheet1 needs to pull only entries which aren't already in db, hence no duplication.
    Hope I've managed to explain myself

    Sub l_feed()
    With Worksheets("l_feed")
    Dim rng As Range, r As Range
    Set rng = .Range("a1:a" & .UsedRange.Rows.Count)
    For Each r In rng
    if r.Value 'here r.value should check if it's not already in a certain range in the dbsheet... 
    Next r
    End With
    End Sub
    Please * if you like the answer

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy entry into a range if there is no duplication

    Gi, GIS2013,

    maybe like this:
    Sub l_feed()
    With Worksheets("l_feed")
    Dim rng As Range, r As Range
      Set rng = .Range("a1:a" & .UsedRange.Rows.Count)
      For Each r In rng
        If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A:A"), r) = 0 Then
          Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).EntireRow.Value = r.EntireRow.Value
        End If
      Next r
    End With
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Copy entry into a range if there is no duplication

    Hi there Holger, could you please adjust your code in accordance with my sample (I've already done part of this). Cheers.
    Attached Files Attached Files

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy entry into a range if there is no duplication

    Hi, GIS2013,

    sorry but I donīt understand what you want me to do: you asked for a code to compare the values in Columns A of sheets db and feed, thatīs what the code does. This doesnīt seem to be what you are after. So you would have to explain what you really want.

    Ciao,
    Holger

  5. #5
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Copy entry into a range if there is no duplication

    Sure. I should add a sample file in the first place . Anyway, if you look at "db" you can see 8 entries. In "feed" there are currently 2 entries. I need the code to copy from "feed" to "db" any entry upon the condition that it's not already in "db". And so the sub should not copy worksheets("feed").range("a2:c2") since it's already in "db", but it should copy worksheets("feed").range("a3:c3") into a new line in "db" (hence to become the last line). Thanks.

    **"feed"'s entry to be declared non-duplicated (and so be copied) if at least one of its values (in column A or B or C) are not exactly as in "db".
    Last edited by GIS2013; 03-04-2014 at 03:07 PM. Reason: **clarification

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy entry into a range if there is no duplication

    Hi, GIS2013,

    maybe like this:
    Sub l_feed()
    Dim r           As Range
    Dim blnCopy     As Boolean
    Dim var         As Variant
    Dim lngCompare  As Long
    Dim ws          As Worksheet
    
    lngCompare = 1
    
    Set ws = Sheets("db")
    
    With Worksheets("feed")
      For Each r In .Range("a2:a" & .UsedRange.Rows.Count)
        blnCopy = False
        Do Until IsError(var)
          var = Application.Match(CDbl(r.Value), ws.Range("A" & lngCompare & ":A" & ws.Range("A" & Rows.Count).End(xlUp).Row), 0)
          If Not IsError(var) Then
            If r.Offset(0, 1).Value = ws.Cells(var, "B").Value And r.Offset(0, 2).Value = ws.Cells(var, "C").Value Then
              blnCopy = True
            End If
            If blnCopy Then lngCompare = lngCompare + var
          End If
        Loop
        If IsError(var) And Not blnCopy Then
          ws.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(1, 3).Value = r.Resize(1, 3).Value
        End If
      Next r
    End With
    
    Set ws = Nothing
    End Sub
    Ciao,
    Holger

  7. #7
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Copy entry into a range if there is no duplication

    Cheers! always great to learn from 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. [SOLVED] Row detection copy paste duplication
    By ste67@me.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-06-2013, 08:40 PM
  2. VBA - copy cell value duplication issue
    By Dream Ahead in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2012, 03:52 PM
  3. copy a range of data based on one entry
    By armyguy in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 04-08-2009, 01:23 AM
  4. [SOLVED] Local Range Name & Sheet Duplication
    By Kevin H. Stecyk in forum Excel General
    Replies: 0
    Last Post: 08-29-2005, 03:05 PM
  5. Stop Number duplication in a range
    By Nu-bEE in forum Excel General
    Replies: 5
    Last Post: 03-21-2005, 01:06 PM

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