Consulting

Results 1 to 2 of 2

Thread: Using Index Match, adding to the bottom of the row those that are missing

  1. #1
    VBAX Newbie
    Joined
    Jun 2013
    Posts
    5
    Location

    Using Index Match, adding to the bottom of the row those that are missing

    Hi All,

    Having some trouble with a macro. Basically, I am trying to create a program that will look for the CUSIPS that are in Range A in Range B, and if they don't exist in Range B, insert them at the end of Range B. The way I have this setup in excel is this:

    My C column has a list of CUSIPS. In column J, I would like for the User to be able to add a list of cusips and then have the ones that don't already exist in the list of CUSIPS in column C be added at the bottom of the range in C. I have written a macro now that uses the index match, but for some reason it adds all of the cusips that exist in J range to column C, not just the ones that are missing. Can someone take a look and see what I am doing wrong? I can provide a template if necessary. Below is the code that I am using.

    Sub Filler()
    Dim Row As Variant
    Dim NumberOfRows As Long
    Row = 0
    Dim Cell As Variant
    Dim CUSIP_COLUMN As Variant
    Dim Msg As Variant




    Do While Sheets("Regan Inv").Range("J3").Offset(Row, 0) <> ""


    Cell = Range("j3").Offset(Row, 0).Value
    NumberOfRows = Worksheets("Regan inv").Range("CUSIPS").Cells.SpecialCells(xlCellTypeConstants).Count
    If IsError(Application.Index("CUSIPS", Application.Match(Cell, "CUSIPS", 0))) Then
    Cells(NumberOfRows, 3).Offset(2, 0) = Cell
    End If
    Row = Row + 1

    Loop
    End Sub


    Thank you in advance for your help!

    Vasilis

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    several methods can be used.

    below procedure looks for col A cell values in col B and if not found writes to the first blank cell in col B.

    [VBA]
    Sub AddMissingValues()

    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    If Application.CountIf(Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row), Range("A" & i).Value) = 0 Then
    Range("B" & Rows.Count).End(xlUp).Offset(1).Value = Range("A" & i).Value
    End If
    Next

    End Sub
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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