+ Reply to Thread
Results 1 to 7 of 7

Trying to use .Find to create a column of values with no duplicates. Code not working.

Hybrid View

  1. #1
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Trying to use .Find to create a column of values with no duplicates. Code not working.

    Hello,
    This should be a very simple problem for most I think. I'm just not seeing the problem. So basically I need to sort thru the data on columns A:B on Tester and pick out the unique part numbers and their cost and then check column C to see if the part number exists there, if it does not, then add it to column C along with the cost in column D. But for some reason, while the first 2 part numbers seem to work fine, it then starts adding duplicates to column C and I'm at my wits end to figure out why my code is allowing this to happen. Any help is greatly appreciated.



    Option Explicit
    Dim pWB As Workbook, wsHome As Worksheet, wsText As Worksheet
    
    Sub Runner()
    Set pWB = Workbooks(ThisWorkbook.Name)
    Set wsHome = pWB.Worksheets("Home")
    Set wsText = pWB.Worksheets("Texter")
    
    'Start Loop
    wsText.Columns("C:D").ColumnWidth = 20
    wsText.Columns("C:D").HorizontalAlignment = xlCenter
    wsText.Range("$C$1").Value = "Part Number"
    wsText.Range("$D$1").Value = "Part Cost"
    
    Dim I, C, A As Integer
    Dim p_Num As Range
    Dim xNum, xCos As String
    A = wsText.Range("$A" & Rows.Count).End(xlUp).Row
    C = wsText.Range("$C" & Rows.Count).End(xlUp).Row
    
    
    For I = A To 2 Step -1
    
    If wsText.Range("$A$" & I).Value = "Mfr" Then
    
    xNum = wsText.Range("$B$" & Val(I + 1)).Value
    xCos = wsText.Range("$B$" & Val(I + 5)).Value
    
    C = wsText.Range("$C" & Rows.Count).End(xlUp).Row
    Set p_Num = wsText.Range("$C$1:$C$" & C).Find(xNum, , xlValues, xlWhole)
    
    If p_Num Is Nothing Then
    wsText.Range("$C$" & Val(C + 1)).Value = xNum
    wsText.Range("$D$" & Val(C + 1)).Value = xCos
    End If
    End If
    
    Next I
    
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Trying to use .Find to create a column of values with no duplicates. Code not working.

    Your data is not very clean:
    853 exists several times because sometimes there is space

    see next code

    xNum = wsText.Range("$B$" & Val(I + 1)).Value
    xNum = trim (xNum)
    - Battle without fear gives no glory - Just try

  3. #3
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: Trying to use .Find to create a column of values with no duplicates. Code not working.

    Thank you so much! That fixed it.

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Trying to use .Find to create a column of values with no duplicates. Code not working.

    You're welcome

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,734

    Re: Trying to use .Find to create a column of values with no duplicates. Code not working.

    How about
    Sub Runner()
       Dim Dic As Object
       Dim Cl As Range
       Set pWB = ThisWorkbook
       Set wsHome = pWB.Worksheets("Home")
       Set wsText = pWB.Worksheets("Texter")
       
       'Start Loop
       wsText.Columns("C:D").ColumnWidth = 20
       wsText.Columns("C:D").HorizontalAlignment = xlCenter
       wsText.Range("$C$1").Value = "Part Number"
       wsText.Range("$D$1").Value = "Part Cost"
       
       Set Dic = CreateObject("scripting.dictionary")
       With wsText.Range("A:A")
          .Replace "Mfr", "=xxxMfr", xlWhole, , False, , False, False
          For Each Cl In .SpecialCells(xlFormulas, xlErrors)
             Dic(Trim(Cl.Offset(1, 1).Value)) = Cl.Offset(5, 1).Value
          Next Cl
          .Replace "=xxxMfr", "Mfr", xlWhole, , False, , False, False
       End With
       wsText.Range("C2").Resize(Dic.Count, 2).Value = Application.Transpose(Array(Dic.keys, Dic.items))
    End Sub

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Trying to use .Find to create a column of values with no duplicates. Code not working.

    Nice code Fluff13 ...!

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,734

    Re: Trying to use .Find to create a column of values with no duplicates. Code not working.

    Thanks for that & the Rep

+ 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] Find matching values then go to 3rd column of worksheet with duplicates and get that value
    By ButteredToast in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-05-2018, 10:09 PM
  2. Replies: 3
    Last Post: 07-09-2016, 08:05 AM
  3. Replies: 11
    Last Post: 02-05-2015, 07:13 PM
  4. [SOLVED] Find a value in a column and copy all data above that row code not working.
    By klunker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2014, 08:44 AM
  5. [SOLVED] Macro to find duplicates, concatenate Unique Values, then delete old duplicates
    By lesoies in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2013, 04:32 PM
  6. [SOLVED] Find duplicates in column A, add values from column B (possibly delete duplicates)
    By luarwhite in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-12-2013, 04:34 PM
  7. code to find text, offset 1 column and paste to new workbook not working
    By trillium in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-10-2011, 07:55 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