InStr problem

daverunt

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

I am using Instr to compare filenames in a directory against those in an array.
Unfortunately because some of the files have the same name apart from '- ST' they are seen as the same by the InStr func.

I could reverse their positions and this does work but I wanted to keep the file order as it is.

Any suggetions? Is their another function I can use to match filenames in the array?

Code:
'Array of file names
arrFile = Array[COLOR=red]("File1", [/COLOR][COLOR=darkgreen]"[COLOR=red]File1[/COLOR] [/COLOR][COLOR=darkred][B]- ST[/B]",[/COLOR] [COLOR=blue]"File2", [/COLOR][COLOR=purple][COLOR=blue]"File2[/COLOR] [/COLOR][COLOR=darkred][B]- ST",[/B][/COLOR] "File3", _
"File4", "File5")
 
SampleFile = Dir("*.dat")
 
Do While SampleFile <> ""
 
For arrIndex = LBound(arrFile) To UBound(arrFile)
If InStr(SampleFile, arrFile(arrIndex)) > 0 Then
 
FileMatched = True
 
Select Case arrIndex
 
Case Is = 0
Rng = "B2"
Case Is = 1
Rng = "B3"
Case Is = 2
Rng = "B4"
Case Is = 3
Rng = "B5"
Case Is = 4
Rng = "B7"
Case Is = 5
Rng = "B11"
Case Is = 6
Rng = "B12"
End Select
[/Code
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Ignore that I found a method

Code:
 If (StrComp(SampleFile, arrFile(arrIndex), vbTextCompare) = 0) Then
 
Upvote 0
What's the problem?

Are matches not being found when you know there are matches?

There seems to be some code missing from what you've posted I think.
 
Upvote 0
Hi Norrie, the opposite, matches were being found when I didn't want them.

When it comes across the file called 'File1 - ST' in the directory
InStr is returning a match for the first array index, 0 because it sees 'File1' in 'File1-ST'. (I wanted it to return a match for array index 1).

Because 'File1' is also in the directory I end up with Select Case = 0 for both files.


Anyway thanks a lot for looking - I found the Compare works as I need it.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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