VBA selection File

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.