Arrgh Arrays

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,946
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I have some basic code to create an array of filenames in a folder. All good.
I remove the timestamps and extensions from the files, which leaves small groups of files with the exact same name.

eg:

Dave
Dave
Dave
Fred
Fred
Fred
John
John
John
etc

These are all added to the array.
However I need each name to appear only once in the array.

At what point do I check whether the name already exists in the array?
Can it be done before adding the name to the array?
Or does it have to be done after it has been preserved?

Code:
Sub Create_Array()

Dim MyFile As String
Dim Counter As Long

Dim DirectoryListArray() As String
ReDim DirectoryListArray(50)


MyFile = Dir$("C:\junk\357536080015052\*.res")
Do While MyFile <> ""

    ResFileName = (Left(MyFile, Len(MyFile) - 22)) ' This means there are several files with the same name once the timestamps are removed.
    
    DirectoryListArray(Counter) = ResFileName
       
    MyFile = Dir$
    Counter = Counter + 1
    
Loop

ReDim Preserve DirectoryListArray(Counter - 1)


'Print the array content to check
For Counter = 0 To UBound(DirectoryListArray)
 
    Debug.Print DirectoryListArray(Counter)
Next Counter
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi daverunt,

try changing

VBA Code:
Do While MyFile <> ""
    ResFileName = (Left(MyFile, Len(MyFile) - 22)) ' This means there are several files with the same name once the timestamps are removed.    
    DirectoryListArray(Counter) = ResFileName       
    MyFile = Dir$
    Counter = Counter + 1    
Loop

to

VBA Code:
Do While MyFile <> ""
  ResFileName = (Left(MyFile, Len(MyFile) - 22)) ' This means there are several files with the same name once the timestamps are removed.
  If IsError(Application.Match(ResFileName, DirectoryListArray, 0)) Then
    DirectoryListArray(Counter) = ResFileName
    Counter = Counter + 1
  End If
  MyFile = Dir()
Loop

Ciao,
Holger
 
Upvote 0
Solution
Another option is to use a dictionary
VBA Code:
Sub Create_Array()

Dim MyFile As String
Dim Counter As Long

Dim DirectoryListArray As Object
Set DirectoryListArray = CreateObject("scripting.dictionary")


MyFile = Dir$("C:\junk\357536080015052\*.res")
Do While MyFile <> ""

    ResFileName = (Left(MyFile, Len(MyFile) - 22)) ' This means there are several files with the same name once the timestamps are removed.
    
    DirectoryListArray(ResFileName) = Empty
       
    MyFile = Dir$
    Counter = Counter + 1
    
Loop



'Print the array content to check
For Counter = 0 To DirectoryListArray.Count - 1
 
    Debug.Print DirectoryListArray()(Counter)
Next Counter
End Sub
 
Upvote 0
Another option is to use a dictionary
VBA Code:
Sub Create_Array()

Dim MyFile As String
Dim Counter As Long

Dim DirectoryListArray As Object
Set DirectoryListArray = CreateObject("scripting.dictionary")


MyFile = Dir$("C:\junk\357536080015052\*.res")
Do While MyFile <> ""

    ResFileName = (Left(MyFile, Len(MyFile) - 22)) ' This means there are several files with the same name once the timestamps are removed.
   
    DirectoryListArray(ResFileName) = Empty
      
    MyFile = Dir$
    Counter = Counter + 1
   
Loop



'Print the array content to check
For Counter = 0 To DirectoryListArray.Count - 1
 
    Debug.Print DirectoryListArray()(Counter)
Next Counter
End Sub
Hi Fluff,

this errors on the debug.print line - wrong num arguments/invalid property assignment so I used DirectoryListArray(Counter)

However there's nothing then printing to the window.
I don't see where it is adding the unique file name to the array?
 
Upvote 0
Oops, missed a bit, it should be
VBA Code:
    Debug.Print DirectoryListArray.Keys()(Counter)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,619
Members
452,661
Latest member
Nonhle

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