Private Function selectfile (ByVal strfilter As String) as String
Dim FileName as Variant
The file name returned by the Open File dialog box is a full path file name, and the value may be false, so the type is variant
Dim sfilename as String ' filename extracted from filename
Dim spathname as String ' path name extracted from filename
Dim afile as Variant ' array, using when extracting filename sfilename
' Call the Windows Open File dialog box
FileName = Application.getopenfilename (strfilter) ' "CSV file (*.csv), *.csv"
If FileName = False Then ' if press ' Cancel ' key
Selectfile = ""
Else
Afile = Split (FileName, "\") ' in full path, with ' \ ' as delimiter, divided into data
Spathname = afile (0) ' Fetch letter
For i = 1 to UBound (afile)-1 ' Loop compound path name
Spathname = spathname & "\" & Afile (i)
Next
Selectfile = spathname & "\" & Afile (UBound (afile))
End If
End Function
Sub Test ()
Dim txt As Object
Dim lrow As Long
Dim text As String
Dim Filename As Variant
ChDir thisworkb Ook. Path
Filename = application.getopenfilename ("Text Files (*.txt), *.txt", "Select Profile", Multiselect:=true)
For each FN in Filename
PGE = PGE + 1
lrow = 0
Set FSO = CreateObject ("Scripting . FileSystemObject ")
Set txt = FSO. OpenTextFile (FN, ForReading, False)
do Until txt. AtEndOfStream
lrow = lrow + 1
text = txt. ReadLine
thisworkbook.worksheets (PGE). Cells (Lrow, 1) = text
Loop
txt. Close
Set txt = Nothing
Next
End Sub
VBA Selection File