• VBA 选择文件


    Private Function SelectFile(ByVal strFilter As String) As String
        Dim FileName As Variant
         '打开文件对话框返回的文件名,是一个全路径文件名,其值也可能是False,因此类型为Variant
        Dim sFileName As String                         '从FileName中提取的文件名
        Dim sPathName As String                         '从FileName中提取的路径名
        Dim aFile As Variant                            '数组,提取文件名sFileName时使用
         '调用Windows打开文件对话框
        FileName = Application.GetOpenFilename(strFilter) ' "CSV 文件 (*.csv),*.csv"
       
        If FileName = False Then                       '如果按“取消”键
            SelectFile = ""
        Else
            aFile = Split(FileName, "")                '在全路径中,以“”为分隔符,分成数据
            sPathName = aFile(0)                        '取盘符
            For i = 1 To UBound(aFile) - 1              '循环合成路径名
                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 ThisWorkbook.Path
    Filename = Application.GetOpenFilename("Text Files (*.txt), *.txt", , "请选取档案", , 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

  • 相关阅读:
    解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat
    安装KubeSphere
    kubesphere 安装
    正则表达式在线测试
    爬虫与Python:(二)Python基础篇——扩展:实现九九乘法表
    为什么 Python 的 Range 要设计成左开右闭区间?
    Python内置函数之range()
    爬虫与Python:(二)Python基础篇——13.类
    爬虫与Python:(二)Python基础篇——12.函数
    CSS之text-align
  • 原文地址:https://www.cnblogs.com/lbnnbs/p/4784997.html
Copyright © 2020-2023  润新知