Thanks shg. I tried the code you posted but for some reason regardless of the folder I chose, it gave me only the txt files on C:\. I don't know why its ignoring my decision and defaulting to C:\.
I have been working on it all day and make a breakthrough (at least a small one) and got the original code to work. Here is the modified code:
Option Explicit
Sub LoopThroughFiles()
Dim sFPath As String
Dim sFName As String
Dim sFNamePath As String
sFPath = (BrowseForFolder & "\")
sFName = Dir$(sFPath & "*.txt")
Debug.Print sFName
Do While sFName <> ""
MsgBox (sFName)
'Get next file name
sFName = Dir$
sFNamePath = sFPath + sFName
Loop
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level
Dim ShellApp As Object
'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
'Destroy the Shell Application
Set ShellApp = Nothing
'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False
End Function
Even though, it works only to give me the file names in a msg box. When I try to combine it with other code, I get an error saying that the file does not exist. Below is an example which incorporates the code above with the code that you generously created for me in a previous thread.
Sub LoopThroughFiles()
Dim sFPath As String
Dim sFName As String
Dim sFNamePath As String
Dim iFF As Integer
Dim sInp As String
Dim asDat(1 To 3) As String
Dim iRow As Long
sFPath = (BrowseForFolder & "\")
sFName = Dir$(sFPath & "*.txt")
Debug.Print sFName
Do While sFName <> ""
iFF = FreeFile
Open sFName For Input As #iFF
Do While Not EOF(iFF)
Line Input #iFF, sInp
Debug.Print Left(sInp, 6)
Select Case Left(sInp, 6)
Case "TY - "
asDat(2) = Mid(asDat(2), 7)
If iRow <> 0 Then Cells(iRow, "B").Value = Join(asDat, ", ")
Erase asDat
Case "ID - "
iRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(iRow, "A").Value = Mid(sInp, 7)
Case "A1 - "
If Len(asDat(1)) = 0 Then asDat(1) = Mid(sInp, 7)
Case "JF - "
asDat(2) = sInp
Case "JO - "
Select Case Left(asDat(2), 6)
Case "JA - ", "J1 - ", "J2 - ", vbNullString
asDat(2) = sInp
End Select
Case "JA - "
Select Case Left(asDat(2), 6)
Case "J1 - ", "J2 - ", vbNullString
asDat(2) = sInp
End Select
Case "J1 - "
Select Case Left(asDat(2), 6)
Case "J2 - ", vbNullString
asDat(2) = sInp
End Select
Case "J2 - "
Select Case Left(asDat(2), 6)
Case vbNullString
asDat(2) = sInp
End Select
Case "Y1 - "
asDat(3) = Mid(sInp, 7, 4)
Case "T1 - "
Cells(iRow, "C").Value = Mid(sInp, 7)
Case "N2 - "
Cells(iRow, "D").Value = Mid(sInp, 7)
Case "KW - "
With Cells(iRow, "E")
If IsEmpty(.Value) Then
.Value = Mid(sInp, 7)
Else
.Value = .Value & "; " & Mid(sInp, 7)
End If
End With
End Select
Loop
If IsEmpty(Cells(iRow, "B")) Then
asDat(2) = Mid(asDat(2), 7)
Cells(iRow, "B").Value = Join(asDat, ", ")
End If
Close iFF
'Get next file name
sFName = Dir$
sFNamePath = sFPath + sFName
Loop
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level
Dim ShellApp As Object
'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
'Destroy the Shell Application
Set ShellApp = Nothing
'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False
End Function
I don't know why it doesn't work even though I got to work earlier today, but like an idiot I forgot to save the file and I can't get it to work again. I have been pulling my hair out at the roots because I am so frustrated with this. I hope you can help me resolve this issue.
Thanks again.
abousetta
Bookmarks