I have a program that will look through many text files, in a folder, to seek out a specific line and then put it into an excel sheet. I would like to only select one text file from a directory, any ideas? Thanks for your help!
The code I have is:
Function PickFolder(strStartDir As Variant) As String
'Selecting a folder to gather the text files and put into one excel spreadsheet.
Dim SA As Object, F As Object
Set SA = CreateObject("Shell.application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.Items.Item.Path
End If
Set F = Nothing
Set SA = Nothing
End Function
Sub Rate()
Dim fso As Object, F As Object, Path As String
Dim CurrentRow As Range
Dim TextLine As String, NumCols As String
Dim CurWkb as string
On Error Resume Next
Application.ScreenUpdating = False
UserFile = PickFolder(strStartDir)
If UserFile = "" Then
MsgBox "Canceled"
Exit Sub
End If
Set CurWkb = Workbooks.Add
Set fso = CreateObject("Scripting.FileSystemObject")
For Each F In fso.GetFolder(UserFile).Files
If F.Type = "Text Document" Then
TheTextFile = UserFile & "\" & F.Name
FF = FreeFile()
Open TheTextFile For Input As FF
While Not EOF(FF)
Line Input #FF, TextLine
'gets the method file
If Right(TextLine, 5) = "pro" Then
Range("A65536").End(xlUp).Offset(1, 0) = TextLine
End If
cnt = cnt + 1
Wend
Close #FF
End If
Next
The code I have is:
Function PickFolder(strStartDir As Variant) As String
'Selecting a folder to gather the text files and put into one excel spreadsheet.
Dim SA As Object, F As Object
Set SA = CreateObject("Shell.application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.Items.Item.Path
End If
Set F = Nothing
Set SA = Nothing
End Function
Sub Rate()
Dim fso As Object, F As Object, Path As String
Dim CurrentRow As Range
Dim TextLine As String, NumCols As String
Dim CurWkb as string
On Error Resume Next
Application.ScreenUpdating = False
UserFile = PickFolder(strStartDir)
If UserFile = "" Then
MsgBox "Canceled"
Exit Sub
End If
Set CurWkb = Workbooks.Add
Set fso = CreateObject("Scripting.FileSystemObject")
For Each F In fso.GetFolder(UserFile).Files
If F.Type = "Text Document" Then
TheTextFile = UserFile & "\" & F.Name
FF = FreeFile()
Open TheTextFile For Input As FF
While Not EOF(FF)
Line Input #FF, TextLine
'gets the method file
If Right(TextLine, 5) = "pro" Then
Range("A65536").End(xlUp).Offset(1, 0) = TextLine
End If
cnt = cnt + 1
Wend
Close #FF
End If
Next