+ Reply to Thread
Results 1 to 13 of 13

return missing values

Hybrid View

  1. #1
    Registered User
    Join Date
    11-27-2012
    Location
    BG
    MS-Off Ver
    Excel 2003
    Posts
    21

    return missing values

    Hi,

    I have two worksheets every with two column.
    In the first sheet in Column A are 4digit codes, in Column B should be filled in required information(missing indexes) .
    Sheet 2 is based on report for these 4 digit codes(column A) with existing indexes for these codes.
    The values for the indexes are between 1-9999 and they were not used sequentially.
    For example for code 1685 we can see in sheet2 that used indexes are 1,2,5,30,998 so in sheet 1 should be returned 3,4,6.

    Can you please suggest vba code that return unused indexes?

    Regards,
    Guerolito
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: return missing values

    You didn't mention how you want the result.
    Sub test()
        Dim a, i As Long, e, s, dic As Object, msg As String
        Set dic = CreateObject("Scripting.Dictionary")
        With CreateObject("Scripting.Dictionary")
            For Each e In Array("Sheet2", "Sheet1")
                a = Sheets(e).Range("b2").CurrentRegion.Value
                For i = 2 To UBound(a, 1)
                    If e = "Sheet2" Then
                        If Not .exists(a(i, 1)) Then
                            Set .Item(a(i, 1)) = _
                            CreateObject("Scripting.Dictionary")
                        End If
                        .Item(a(i, 1))(a(i, 2)) = Empty
                    Else
                        If Not .exists(a(i, 1)) Then
                            If Not dic.exists(a(i, 1)) Then
                                Set dic(a(i, 1)) = _
                                CreateObject("Scripting.Dictionary")
                            End If
                            dic(a(i, 1))(a(i, 2)) = Empty
                        Else
                            If Not .Item(a(i, 1)).exists(a(i, 2)) Then
                                msg = msg & vbLf & a(i, 1) & " : " & a(i, 2)
                            End If
                        End If
                    End If
                Next
            Next
        End With
        If dic.Count <> 0 Then
            For Each e In dic
                For Each s In dic(e)
                    msg = msg & vbLf & e & " : " & s
                Next
            Next
        End If
        MsgBox msg, , "Missing index"
    End Sub

  3. #3
    Registered User
    Join Date
    11-27-2012
    Location
    BG
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: return missing values

    Thank you Jindon,

    The results should be returned in sheet1 column "Index".

    Regards,
    Guerolito

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: return missing values

    Can you show me how exactly you want the result?

  5. #5
    Registered User
    Join Date
    11-27-2012
    Location
    BG
    MS-Off Ver
    Excel 2003
    Posts
    21
    Result should looks like as in attached example file - sheet1 column index.

    Regards,
    Gguerolito

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: return missing values

    You know the result in message box is exactly the same as what is in Sheet1.

    So, no need to change Sheet1.

    Is that what you want?

  7. #7
    Registered User
    Join Date
    11-27-2012
    Location
    BG
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: return missing values

    I think we are almost there but probably I didn’t explain correctly situation so I’ll try again.
    In the example file I filled in Sheet1 “index” with the required indexes manually in order to explain requirements better, but normaly at the beginning I have only codes in Sheet1, index column is blank.
    I filter codes using Advanced filter in order to get unique codes. Then I run report for this unique codes to find already used indexes. The result of report is in Sheet2.
    And then I need VBA code to return in Sheet1 column “Index”
    indexes which have not been used yet.

    Regards,
    Guerolito

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: return missing values

    What do you mean by
    Quote Originally Posted by Guerolito
    indexes which have not been used yet.

  9. #9
    Registered User
    Join Date
    11-27-2012
    Location
    BG
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: return missing values

    indexes which have not been used yet = These are figures between 1-9999 which not exist in combination with respective Code in Sheet2

    Regards,
    Guerolito

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: return missing values

    You mean like this?
    Option Explicit
    
    Sub test()
        Dim a, i As Long, ii As Long, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        a = Sheets("sheet2").Range("b2").CurrentRegion.Value
        For i = 2 To UBound(a, 1)
            If Not dic.exists(a(i, 1)) Then
                Set dic(a(i, 1)) = CreateObject("Scripting.Dictionary")
            End If
            dic(a(i, 1))(a(i, 2)) = Empty
        Next
        With Sheets("sheet1").Range("b2").CurrentRegion
            With .Offset(1)
                .Columns(2).ClearContents
                a = .Value
                For i = 1 To UBound(a, 1)
                    If dic.exists(a(i, 1)) Then
                        For ii = 1 To 9999
                            If Not dic(a(i, 1)).exists(ii) Then
                                a(i, 2) = ii: dic(a(i, 1))(ii) = Empty
                                Exit For
                            End If
                        Next
                    End If
                Next
                .Value = a
            End With
        End With
        Sheets("sheet2").Range("b" & Rows.Count).End(xlUp)(2).Resize(UBound(a, 1), 2).Value = a
    End Sub

  11. #11
    Registered User
    Join Date
    11-27-2012
    Location
    BG
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: return missing values

    Thank you jindon
    It's very close to the solution.
    Just one remark.
    Is it possible not to change the returned indexes after every execution of code.
    I mean if there is no changes in workbook to return the same indexes after every execution.

    Regards,
    Guerolito

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: return missing values

    Just delete one line before End Sub which is
    Sheets("sheet2").Range("b" & Rows.Count).End(xlUp)(2).Resize(UBound(a, 1), 2).Value = a

  13. #13
    Registered User
    Join Date
    11-27-2012
    Location
    BG
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: return missing values

    PERFECT!
    It seems that works as just like I want.
    Now I should understand how the code works.
    Again Thank you!
    Guerolito

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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