zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Windows 8的语音识别
    硬件驱动程序的知识点滴
    怎么将一张100KB以上大小的电子图片压缩成30KB以内
    Hadoop概念学习系列之Hadoop新手学习指导之入门需知(二十)
    Hadoop概念学习系列之Hadoop、Spark学习路线(很值得推荐)(十八)
    研究生,别再玩了,你玩不起!
    redis源代码解读之内存管理————zmalloc文件
    线段树之入门篇
    C#托付和事件
    dlmalloc 2.8.6 源代码具体解释(6)
  • 原文地址:https://www.cnblogs.com/lbnnbs/p/4784997.html
Copyright © 2011-2022 走看看