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
 
Following up on Holger's code. There's an issue if I want to use a dynamic array. This is obviously better as the number of the files may vary.
There's an invalid procedure call on the ApplicationMatch line. I assume because the array is empty initially?

So I've added x as a counter to Redim the array before using ApplicationMatch, which works.

Any problem with what I am doing here.

Code:
Dim MyFile As String
Dim Counter As Long

Dim DirectoryListArray()

'https://www.mrexcel.com/board/threads/arrgh-arrays.1221243/#post-5972187

MyFile = Dir("C:\junk\357536080015052\*.res")
 x = 0

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


For Counter = 0 To UBound(DirectoryListArray)
    Debug.Print DirectoryListArray(Counter)
Next Counter
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
There's an issue if I want to use a dynamic array. This is obviously better as the number of the files may vary.
You won't have that problem using a Dictionary as it will be as large as it needs to be & no more. ;)
 
Upvote 0
You won't have that problem using a Dictionary as it will be as large as it needs to be & no more. ;)
Yes I can see that but It's just knowing, as I took the time to find a work around.
 
Upvote 0
Hi daverunt,

just using one variable for the array and setting the lower bound to 1 (just a sample) :

VBA Code:
Option Base 1

Sub Create_Array()
'https://www.mrexcel.com/board/threads/arrgh-arrays.1221243
Dim MyFile                  As String
Dim ResFileName             As String
Dim Counter                 As Long
Dim DirectoryListArray()    As String

ReDim DirectoryListArray(1)

MyFile = Dir$("C:\junk\357536080015052\*.res")

Counter = 1
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
    ReDim Preserve DirectoryListArray(Counter)
  End If
  MyFile = Dir()
Loop

ReDim Preserve DirectoryListArray(Counter - 1)

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

Thanks for the feedback.

Holger
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,613
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