substring in vba ,code does not read the first excel file name substring

hfs

Registered User.
Local time
Today, 00:09
Joined
Aug 7, 2013
Messages
47
I have a folder with 3 excel files ,and i want to read the files with names,and get a substring from the name of excel file,My code is working fine but the issue is when it reads the substring,it does not read the very first file ,the code is here :

Code:
strFile = Dir(strPath & "*.xlsm")
 
 While strFile <> ""
  intFile = intFile + 1
  ReDim Preserve strFileList(1 To intFile)
  strFileList(intFile) = strFile
[COLOR=red]strFile = Dir()[/COLOR]
 
 
'Reads the substring(the Lot# from the name of the file)
intPos = InStr(1, strFile, ")")
If intPos > 0 Then
    ' : found, so take up to :
    strResult = Left(strFile, intPos - 1)
    MsgBox (strResult)
Else
    ' : not found, so take whole string
    strResult = strFile
 '   MsgBox (strResult)
End If

Wend

when i debug,it reads the first file till strFile = Dir() after that it goes to the second excel file and leaves the first file.I need help here .Thanks
 
Last edited:
Maybe don't run the Dir() command in your highlighted line until after the If...Else...End If block.
 
Thankyou so VERY much .That Worked :)
 

Users who are viewing this thread

Back
Top Bottom