bumbum2812
New Member
- Joined
- Sep 7, 2020
- Messages
- 26
- Office Version
- 365
- Platform
- Windows
Hi Experts,
I'm having below code to take a list of filename to my sheet, i want to choose file extension ".PDF" which is form the right so i use the InStrRev then want to take specific name form the left so i use InStr but it did not return what i want. thanks in advance.
I'm having below code to take a list of filename to my sheet, i want to choose file extension ".PDF" which is form the right so i use the InStrRev then want to take specific name form the left so i use InStr but it did not return what i want. thanks in advance.
VBA Code:
Sub SearchFiles()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim n As Long
Dim folderPath As String
Dim lr As Long
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Ref")
lr = ws.Range("C" & Rows.Count).End(xlUp).Row
folderPath = Application.ActiveWorkbook.Path
'MsgBox folderPath
ws.Range("C2:C" & lr).ClearContents
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.getfolder(folderPath)
For Each oFile In oFSO.getfolder(folderPath).Files
If InStrRev(oFile, ".pdf") Or InStrRev(oFile, ".PDF") Then n = n + 1
If InStr(oFile, "SGN_") Or InStr(oFile, "HAN_") Or InStr(oFile, "MAWB") Or InStr(oFile, "MANIFEST") Or InStr(oFile, "mawb") Or InStr(oFile, "Manifest") Then
n = n + 1
Sheets(1).Cells(n + 1, 3).Value = Array(oFile.Name)
End If
Next
End Sub