標題:
[發問]
如何匯入多個資料夾文字檔內容
[打印本頁]
作者:
luke
時間:
2012-3-28 22:24
標題:
如何匯入多個資料夾文字檔內容
小弟最近剛學VBA,
我想從指定目錄D:\TEST, 按下" 匯入"按鈕,
自動取出資料夾裡的文字檔(*.ccc)第一列內容, 複製放入test.xls中的Sheets("IN"), 由G2開始依次匯入資料.
以下是我用巨集錄製方式只錄4個資料夾
麻煩請教先進!!
Sub Macro1()
'
' Macro1 Macro
'
' 快速鍵: Ctrl+k
'
Sheets("Sheet1").Select
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\TEST\TT_00011111\00011112.ccc", Destination:=Range("$A$1"))
.Name = "00011112"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2)
.TextFileFixedColumnWidths = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Selection.Copy
Sheets("IN").Select
Range("G2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\TEST\TT_00012345\12345678.ccc", Destination:=Range("$A$1"))
.Name = "12345678"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2)
.TextFileFixedColumnWidths = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Selection.Copy
Sheets("IN").Select
Range("G3").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\TEST\TT_00022222\00333333.ccc", Destination:=Range("$A$1"))
.Name = "00333333"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2)
.TextFileFixedColumnWidths = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Selection.Copy
Sheets("IN").Select
Range("G4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\TEST\TT_00023456\25153654.ccc", Destination:=Range("$A$1"))
.Name = "25153654"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2)
.TextFileFixedColumnWidths = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Selection.Copy
Sheets("IN").Select
Range("G5").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A1").Select
Cells.Select
Selection.QueryTable.Delete
Selection.QueryTable.Delete
Selection.QueryTable.Delete
Selection.QueryTable.Delete
Selection.ClearContents
Sheets("IN").Select
Range("A1").Select
End Sub
作者:
Hsieh
時間:
2012-3-28 23:00
回復
1#
luke
Sub 匯入()
Dim ary() As String, rw As Long, Mystr$, MyPath$
rw = 2: i = 0
path1 = "D:\TEST\"
file1 = Dir(path1 & "*.*", vbDirectory) '只處理資料夾
Do While file1 <> ""
If file1 <> "." And file1 <> ".." And _
GetAttr(path1 & file1) = vbDirectory Then
i = i + 1
ReDim Preserve ary(i)
ary(i) = file1
End If
file1 = Dir
Loop
For i = 1 To UBound(ary)
Cells(rw, 1) = ary(i)
MyPath = path1 & ary(i) & "\"
fs = Dir(MyPath & "*.ccc")
Open MyPath & fs For Input As #1
Line Input #1, Mystr
Cells(rw, 7) = Mid(Mystr, 2)
Close #1
rw = rw + 1
Next i
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)