標題:
[發問]
如何從不同資料夾匯入文字檔
[打印本頁]
作者:
luke
時間:
2012-4-15 12:05
標題:
如何從不同資料夾匯入文字檔
各位大大
如何從不同的資料夾匯入文字檔至 sheet1表A:J欄
煩請先進指導!
[attach]10445[/attach]
作者:
register313
時間:
2012-4-15 14:21
回復
1#
luke
Sub xx()
Application.ScreenUpdating = False
Set FT = ActiveWorkbook
For X = 1 To 5
FS = "D:\LAN" & X & "\LAN" & X & ".CSV"
With Workbooks.Open(FS).Sheets("LAN" & X)
FT.Sheets("Sheet1").Columns("A:B").Offset(0, (X - 1) * 2) = .Columns("A:B").Value
.Parent.Close False
End With
Next
Application.ScreenUpdating = True
End Sub
複製代碼
作者:
luke
時間:
2012-4-15 17:33
本帖最後由 luke 於 2012-4-15 17:36 編輯
回復
2#
register313
謝謝R大
若資料有修改如新增資料於sheet1表A:J欄的A11:J17
可否更新轉存回至原資料夾的文字檔
A11:B17欄是新增加資料欲轉存至D:\LAN1資料夾LAN1.csv的1,2欄
C11:D17欄是新增加資料欲轉存至D:\LAN2資料夾LAN2.csv的1,2欄
E11:F17欄是新增加資料欲轉存至D:\LAN3資料夾LAN3.csv的1,2欄
G11:H17欄是新增加資料欲轉存至D:\LAN4資料夾LAN4.csv的1,2欄
I17:J17欄是新增加資料欲轉存至D:\LAN5資料夾LAN5.csv的1,2欄
煩請先進, 大大指導
[attach]10447[/attach]
作者:
register313
時間:
2012-4-15 18:09
回復
3#
luke
Sub 轉入()
Application.ScreenUpdating = False
Set FT = ActiveWorkbook
For X = 1 To 5
FS = "D:\LAN" & X & "\LAN" & X & ".CSV"
With Workbooks.Open(FS).Sheets("LAN" & X)
FT.Sheets("Sheet1").Columns("A:B").Offset(0, (X - 1) * 2) = .Columns("A:B").Value
.Parent.Close False
End With
Next
Application.ScreenUpdating = True
End Sub
Sub 轉出()
Application.ScreenUpdating = False
Set FT = ActiveWorkbook
For X = 1 To 5
FS = "D:\LAN" & X & "\LAN" & X & ".CSV"
With Workbooks.Open(FS).Sheets("LAN" & X)
FT.Sheets("Sheet1").Columns("A:B").Offset(0, (X - 1) * 2).Copy .Columns("A:B")
.Parent.Close savechanges:=True
End With
Next
Application.ScreenUpdating = True
End Sub
複製代碼
作者:
Hsieh
時間:
2012-4-15 22:32
回復
3#
luke
Sub InputCSV() '讀入CSV
Dim ary() As String, rw As Long
i = 0: k = 1
path1 = ThisWorkbook.Path & "\"
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)
path2 = path1 & ary(i) & "\"
fs = Dir(path2 & "*.csv")
Do Until fs = ""
Open path2 & fs For Input As #1
r = 1
Do Until EOF(1)
Line Input #1, mystr
ar = Split(mystr, ",")
Cells(r, k).Resize(, UBound(ar) + 1) = ar
r = r + 1
Loop
k = k + 2
Close #1
fs = Dir
Loop
Next i
End Sub
Sub OutputCSV() '輸出CSV
path1 = ThisWorkbook.Path & "\"
k = 1
Do Until Cells(1, k) = ""
r = 1
fs = path1 & Cells(1, k + 1) & "\" & Cells(1, k + 1) & ".csv"
Open fs For Output As #1
Do Until Cells(r, k) = ""
mystr = Cells(r, k) & "," & Cells(r, k + 1)
Print #1, mystr
r = r + 1
Loop
Close #1
k = k + 2
Loop
End Sub
複製代碼
作者:
luke
時間:
2012-4-15 22:35
回復
4#
register313
謝謝R大
若資料夾新增多了D:\LAN0的LAN0.csv即
A:B兩欄儲存至D:\LAN0資料夾的LAN0.csv
C:D兩欄儲存至D:\LAN1資料夾的LAN1.csv
E:F兩欄儲存至D:\LAN2資料夾的LAN2.csv
G:H兩欄儲存至D:\LAN3資料夾的LAN3.csv
I:J兩欄儲存至D:\LAN4資料夾的LAN4.csv
K:L兩欄儲存至D:\LAN5資料夾的LAN5.csv
煩請先進, 大大指導如何修改?
[attach]10451[/attach]
作者:
register313
時間:
2012-4-15 22:55
回復
6#
luke
Sub 轉入()
Application.ScreenUpdating = False
Set FT = ActiveWorkbook
For X = 0 To 5
fs = "D:\LAN" & X & "\LAN" & X & ".CSV"
With Workbooks.Open(fs).Sheets("LAN" & X)
FT.Sheets("Sheet1").Columns("A:B").Offset(0, X * 2) = .Columns("A:B").Value
.Parent.Close False
End With
Next
Application.ScreenUpdating = True
End Sub
Sub 轉出()
Application.ScreenUpdating = False
Set FT = ActiveWorkbook
For X = 0 To 5
fs = "D:\LAN" & X & "\LAN" & X & ".CSV"
With Workbooks.Open(fs).Sheets("LAN" & X)
FT.Sheets("Sheet1").Columns("A:B").Offset(0, X * 2).Copy .Columns("A:B")
.Parent.Close savechanges:=True
End With
Next
Application.ScreenUpdating = True
End Sub
複製代碼
作者:
luke
時間:
2012-4-15 23:39
回復
5#
Hsieh
謝謝H大
讀入CSV檔案料夾內有類似名稱, 如D:\LAN0資料夾內有LAN0.csv和LAN6.csv兩個文字檔時,
LAN6.csv也會被匯入(只需匯入LAN0.csv)
進行輸出CSV時, 該LAN6.csv找不到路徑會出現錯誤
以上應如何避免錯誤
煩請先進指導
[attach]10453[/attach]
作者:
luke
時間:
2012-4-16 11:38
回復
7#
register313
謝謝R大
若想將轉出後的資料夾(連同文字檔), 按下sheet1表"壓縮"按鈕來分別作資料夾壓縮
如LAN0資料夾壓縮成LAN0.rar
煩請先進, 大大指導
[attach]10455[/attach]
作者:
Hsieh
時間:
2012-4-16 20:25
回復
9#
luke
Sub ZipAsWb2() '壓縮成Zip
'參考crdotlin前輩http://blog.xuite.net/crdotlin/excel/20830799-%E4%B8%80%E6%AC%A1%E6%80%A7%E6%9B%BF%E6%8F%9B
Dim ZipFile, srFolder, nFile, ofile
Dim theShell As Object
'指定來源檔案的資料夾
f = ThisWorkbook.Path
srFolder = f
'檢查資料夾是否存在
Set theShell = CreateObject("Shell.Application")
If theShell.Namespace(srFolder) Is Nothing Then
MsgBox srFolder & " 資料夾不存在!"
End
End If
'檢查是否為空的資料夾
If theShell.Namespace(srFolder).items.Count = 0 Then
MsgBox srFolder & " 資料夾中沒任何檔案存在!"
End
End If
'開啟一個空的Zip壓縮檔案
ZipFile = f & ".zip"
Open ZipFile For Output As #1
'寫入ZIP檔頭
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
'複製每一個在zip檔中的檔案
On Error Resume Next
For Each ofile In theShell.Namespace(srFolder).items
If ofile <> ".zip" Then theShell.Namespace(ZipFile).CopyHere (ofile)
'簡單暫停1秒等候複製完成
Application.Wait Now + 1 / 86400#
Next
End Sub
Sub InputCSV() '讀入CSV
Dim ary() As String, rw As Long
i = 0: k = 1
Cells.ClearContents
path1 = ThisWorkbook.Path & "\"
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)
path2 = path1 & ary(i) & "\"
fs = Dir(path2 & "*.csv")
Do Until fs = ""
If Split(fs, ".")(0) = ary(i) Then
Open path2 & fs For Input As #1
r = 1
Do Until EOF(1)
Line Input #1, mystr
ar = Split(mystr, ",")
Cells(r, k).Resize(, UBound(ar) + 1) = ar
r = r + 1
Loop
k = k + 2
Close #1
End If
fs = Dir
Loop
Next i
End Sub
Sub OutputCSV() '輸出CSV
path1 = ThisWorkbook.Path & "\"
k = 1
Do Until Cells(1, k) = ""
r = 1
fs = path1 & Cells(1, k + 1) & "\" & Cells(1, k + 1) & ".csv"
Open fs For Output As #1
Do Until Cells(r, k) = ""
mystr = Cells(r, k) & "," & Cells(r, k + 1)
Print #1, mystr
r = r + 1
Loop
Close #1
k = k + 2
Loop
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)