display missing values only

Jake975

Board Regular
Joined
Feb 9, 2020
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
this macro displays the correct names from a file list but i need it to display the missing files not the existing ones and ideas of how to change it to make that happen?
VBA Code:
Sub MatchingValues()
    Dim DirectoryListArray As Variant, sPath As String
    Dim rg As Range, i As Long, j As Long
    Dim ListBox1 As Variant
    
    sPath = "C:\Users\jbeehler.fzrpa\Desktop\auto\"
    DirectoryListArray = GetFiles(sPath)
    
    Set rg = Worksheets("Data List").Cells(1, 1).CurrentRegion
    With CreateObject("Scripting.Dictionary")
        For i = 0 To UBound(DirectoryListArray)
            For j = 2 To rg.Rows.Count
                If Not rg.Cells(j, 6) = vbNullString Then
                    If InStr(1, DirectoryListArray(i), rg.Cells(j, 6)) > 0 Then
                        .Item(rg.Cells(j, 3).Value) = Empty
                    End If
                End If
            Next j
        Next i
        ListBox1 = .Keys
    End With
    
    MsgBox Join(ListBox1, vbLf)                   'Adapt to your needs
    
End Sub


Function GetFiles(sPath As String) As Variant
    Dim sFileName As String
    With CreateObject("Scripting.Dictionary")
        sFileName = Dir(sPath, vbNormal)
        Do While Not sFileName = vbNullString
            .Item(sFileName) = Empty
            sFileName = Dir
        Loop
        GetFiles = .Keys
    End With
End Function
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Are you trying to get a list of the files in the directory that are not listed on your sheet, or the otherway round?
 
Upvote 0
The other way around there is a list of partial names in the sheet I need to know from those names what one is missing from the directory
 
Upvote 0
Ok, how about
VBA Code:
Sub MatchingValues()
    Dim DirectoryListArray As Variant, sPath As String
    Dim rg As Range, i As Long, j As Long
    Dim ListBox1 As Variant
    
    sPath = "C:\Users\jbeehler.fzrpa\Desktop\auto\"
    DirectoryListArray = GetFiles(sPath)
    
    Set rg = Worksheets("Data List").Cells(1, 1).CurrentRegion
    With CreateObject("Scripting.Dictionary")
         For j = 2 To rg.Rows.Count
             If Not rg.Cells(j, 6) = vbNullString Then
                 If UBound(Filter(DirectoryListArray, rg.Cells(j, 6), 1, 1)) < 0 Then
                     .Item(rg.Cells(j, 3).Value) = Empty
                 End If
             End If
         Next j
        ListBox1 = .Keys
    End With
    
    MsgBox Join(ListBox1, vbLf)                   'Adapt to your needs
    
End Sub


Function GetFiles(sPath As String) As Variant
    Dim sFileName As String
    With CreateObject("Scripting.Dictionary")
        sFileName = Dir(sPath, vbNormal)
        Do While Not sFileName = vbNullString
            .Item(sFileName) = Empty
            sFileName = Dir
        Loop
        GetFiles = .Keys
    End With
End Function
 
Upvote 0
Solution
Oh thank you so much that worked like just how i needed it do you are a life saver.
If I may ask can you explain it to me because it looks the same to me.
 
Upvote 0
I removed the outer loop through the Directory array
VBA Code:
For i = 0 To UBound(DirectoryListArray)
and then filtered that array based on the cell values
VBA Code:
If UBound(Filter(DirectoryListArray, rg.Cells(j, 6), 1, 1)) < 0 Then
If the cell value is not in the array it returns -1 & then adds the cell value to the dictionary.
 
Upvote 0

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top