【问题标题】:How To Initialize An Array Using Multiple Text Files - VBA如何使用多个文本文件初始化数组 - VBA
【发布时间】:2016-03-15 01:22:31
【问题描述】:

尝试创建一个循环来打开活动工作簿文件夹中的每个 .txt 文件,以便能够从每个 .txt 文件中提取数据。

(来自 .txt 文件的数据将用于初始化数组)


我创建的当前代码给出了代码行的编译错误Invalid Use of Property:
folder2 = ActiveWorkbook.path

我知道folder2 被定义为Folder 数据类型,因此分配字符串ActiveWorkbook.path 变量(以查找当前工作文件夹位置)最有可能导致问题。

浏览活动工作簿文件夹中所有 .txt 文件的正确方法是什么(或引用工作簿文件夹路径的正确方法)?


参考: How to import all text files from a folder


有问题的代码sn-p:

' Loop thru all files in the folder
    folder = ActiveWorkbook.path

    path = folder & "\*.txt"

    Filename = Dir(path)

    ' Get a FileSystem object
    Set fso = New FileSystemObject

    ' get the directory you want
    folder2 = ActiveWorkbook.path

    For Each file In folder.Files

        Set FileText = file.OpenAsTextStream(ForReading)

完整代码:

Option Explicit

Sub Initialize_barcode_lookup_Array()

 Dim fso As FileSystemObject
 Dim folder As String, path As String, count_txt_files As Long, Filename As String
 Dim folder2 As folder
 Dim file As file
 Dim FileText As TextStream
 Dim TextLine As String
 Dim Items() As String
 Dim i As Long, j As Long, k As Long
 Dim cl As Range

 Dim shipping_plan As Long      'Number of shipping plans text files imported
 Dim barcode_lookup() As String
 Dim lastRow As Long
 Dim longest_lastRow As Long
 Dim counter As Long
 Dim FNSKU_Input As String


    'Count the number of files in working directory ( - 1, for the Excel spreadsheet)
        folder = ActiveWorkbook.path

        path = folder & "\*.txt"

        Filename = Dir(path)

        Do While Filename <> ""
            count_txt_files = count_txt_files + 1
                Filename = Dir()
        Loop

        'Range("Q8").Value = count
        MsgBox count_txt_files & " : files found in folder"


    'Define longest_lastRow
    longest_lastRow = 0

    'Define i
    i = 0

    ' Loop thru all files in the folder
    folder = ActiveWorkbook.path

    path = folder & "\*.txt"

    Filename = Dir(path)

    ' Get a FileSystem object
    Set fso = New FileSystemObject

    ' get the directory you want
    folder2 = ActiveWorkbook.path 'fso.GetFolder("D:\YourDirectory\")

    For Each file In folder.Files

        Set FileText = file.OpenAsTextStream(ForReading)

        'Define lastRow
        lastRow = Range("A1").End(xlDown).Row   'Last row of the data set

           'Make sure longest_lastRow is the largest value found of lastRow within all _
           'shipping plan .txt files
           If lastRow > longest_lastRow Then longest_lastRow = lastRow

        'Redimension Array barcode_lookup()
        ReDim barcode_lookup(count_txt_files - 1, longest_lastRow, 9)

        ' Read the file one line at a time
        Do While Not FileText.AtEndOfStream
            TextLine = FileText.ReadLine
            cl = TextLine

            'Initialize Array
                For j = 0 To (lastRow - 1) 'UBound(barcode_lookup(lastRow - 1))
                    For k = 0 To 9
                        barcode_lookup(i, j, k) = cl
                        cl = cl.Offset(0, k + 1).Value
                    Next k
                    'Set cl one row down, and set column back to 0
                    cl = cl.Offset(j + 1, k - 9)
                Next j
        Loop

        ' Clean up
        FileText.Close

        i = i + 1
    Next file

    Set FileText = Nothing
    Set file = Nothing
    Set folder2 = Nothing
    Set fso = Nothing

End Sub

【问题讨论】:

    标签: arrays vba excel


    【解决方案1】:

    尝试使用 GetFolder 命令来获取文件夹:

    set folder2 = fso.getfolder(ActiveWorkbook.path)
    

    由于是对象变量,所以需要Set命令。

    (另请注意,文件夹是 FSO 中的保留名称,因此您可能希望使用不同的变量名称以避免混淆和可能的错误。)

    【讨论】:

    • 感谢 vacip,我开始了解 fso.getfolder() 是如何工作得更好的。这部分代码有效,但现在出现错误,代码如下 19 行,代码 sn-p 'cl = TextLine'。尝试阅读如何解决它 - 有什么建议吗?
    • 很确定我需要将文本文件分解成一个数组,看起来它是文本制表符分隔的。目前正在尝试修改我的代码以使用本指南中的技术:thespreadsheetguru.com/blog/vba-guide-text-files
    • 如何让循环只打开 .txt 文件? (目前它也在打开我正在使用的 excel 文件)
    • cl 是一个范围,你从不设置它。使用类似Set cl=range("A1") 的东西。 那么你可以去给它一个值:cl.Value=TextLine.
    • 要将一个字符串分解为多个子字符串,请使用Split 命令。
    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2012-08-08
    • 2013-03-09
    • 1970-01-01
    • 2012-07-27
    • 2018-06-18
    相关资源
    最近更新 更多