Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting one text file instead of many.

Status
Not open for further replies.

gjsala

Technical User
Feb 20, 2003
107
US
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
 



Hi,

You are looping thru a folder and opening many files it seems.

So WHICH file DO you want to open?

Otherwise use the GetOpenFileName method to have the user select ONE file to open.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,
Thanks for the response is there an exampe of the GetOpenFileName method?

GS
 


Did you try VBA HELP?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top