2016-03-15 128 views
1

嘗試創建一個循環以打開活動工作簿文件夾中的每個.txt文件,以便能夠從每個.txt文件中提取數據。如何使用多個文本文件初始化陣列 - VBA

(從.txt文件的數據將被用於初始化一個數組)


我創建當前代碼給出編譯錯誤Invalid Use of Property爲行代碼:
folder2 = ActiveWorkbook.path

我知道folder2是定義爲Folder數據類型,因此分配字符串ActiveWorkbook.path變量(查找當前工作文件夾位置)最有可能導致此問題。

通過活動工作簿文件夾中的所有.txt文件(或引用工作簿文件夾路徑的正確方法)的正確方法是什麼?


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


代碼片斷與問題:

' 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 

回答

1

嘗試的GetFolder命令拿起文件夾:

set folder2 = fso.getfolder(ActiveWorkbook.path) 

由於它是一個對象變量,你需要設置的命令。

(另請注意,文件夾是在FSO保留名稱,因此您可能需要使用不同的變量名只是爲了避免混淆和可能的錯誤。)

+0

感謝vacip,我開始明白如何fso.getfolder ()的效果好一點。代碼的這一部分起作用,但現在在下面19行中出現錯誤,代碼片段「cl = TextLine」。試圖閱讀如何解決它 - 任何機會的任何建議? – sikorloa

+0

很確定我需要將文本文件分解成一個數組,看起來像是文本標籤分隔。目前試圖修改我的代碼以使用本指南中的技術:http://www.thespreadsheetguru.com/blog/vba-guide-text-files – sikorloa

+0

如何讓循環只打開.txt文件? (目前它也打開我正在從事的excel文件) – sikorloa