返回列表 上一主題 發帖

[發問] 如何拆檔和結合新插入的指定文字檔

本帖最後由 GBKEE 於 2012-6-4 12:06 編輯

回復 1# luke
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Ar, E As Variant, xi As Integer, xlCsv As String, xlPath As String
  4.     Dim Sh(1 To 2) As Worksheet
  5.     xlPath = ThisWorkbook.Path & "\"                                '->修改為正確的檔案路徑
  6.     Set Sh(1) = Workbooks.Open(xlPath & "test21.csv").Sheets(1)
  7.     Set Sh(2) = Sh(1).Parent.Sheets.Add
  8.     Sh(1).Cells.Copy Sh(2).Cells(1)                                 '複製 test21.csv 的資料                             '
  9.     xlCsv = Dir(xlPath & "*.Csv")                                   '尋找 *.Csv檔案
  10.     Do While xlCsv <> "" And LCase(xlCsv) <> "test21.csv"
  11.         With Workbooks.Open(xlPath & xlCsv).Sheets(1)
  12.             Sh(2).Cells(Rows.Count, 1).End(xlUp).Offset(2) = "[*" & xlCsv & "*]"
  13.             .[a1].CurrentRegion.Copy Sh(2).Cells(Rows.Count, 1).End(xlUp).Offset(1)
  14.             .Parent.Close 0
  15.         End With
  16.         xlCsv = Dir
  17.     Loop
  18.      With Sh(2)
  19.         .Activate
  20.         For Each E In ActiveWorkbook.Names
  21.             '刪除所有已定義的名稱 以避免 : 定義的名稱中有不在的 *.Csv
  22.              E.Delete
  23.         Next
  24.        '*** 處裡 已匯入的 *.Csv  *********
  25.         Ar = .Range("a:a").Value
  26.        .Range("a:a").Replace "[*.*]", "=1/0"                                '[*.Csv] 替代為錯誤值
  27.        .Range("a:a").SpecialCells(xlCellTypeFormulas, xlErrors).Select      '選擇有錯誤值的儲存格
  28.         .Range("a:a").Value = Ar                                            '複原原來的值
  29.         For Each E In Selection
  30.             E.CurrentRegion.Name = Replace(Replace(E, "*]", ""), "[*", "")
  31.             '每一儲存格的延伸範圍: 定義名稱  *.Csv
  32.         Next
  33.         '****************************
  34.         Sh(1).Cells.Clear      'test21.csv.Sheets(1) :清除所有資料 重新匯入排序後的*.Csv
  35.         For Each E In ActiveWorkbook.Names             '定義名稱 :會自動排序名稱
  36.             xi = Sh(1).Cells(Rows.Count, 1).End(xlUp).Row
  37.             xi = IIf(xi = 1, 1, xi + 2)
  38.             Range(E.Name).Copy Sh(1).Cells(xi, 1)
  39.             xi = Sh(1).Cells(Rows.Count, 1).End(xlUp).Row
  40.             Sh(1).Cells(xi + 2, 1) = "[*div*]"
  41.         Next
  42.         Application.DisplayAlerts = False
  43.         .Delete                                         '刪除工作表
  44.         Application.DisplayAlerts = True
  45.     End With
  46.     '*****  測試 成功後 解除註解 可存檔
  47.     'Sh(1).Parent.Close True
  48. End Sub
複製代碼

TOP

回復 3# luke
BB-1.csv  ->  BB_1.csv

TOP

本帖最後由 GBKEE 於 2012-6-5 21:34 編輯

回復 5# luke
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Sh(1 To 2) As Worksheet, Ar, E As Variant, xlCsv As String, xlPath As String
  4.     Dim xi As Integer, xR As Integer, xF As Integer
  5.     xlPath = ThisWorkbook.Path & "\"                                                '->修改為正確的檔案路徑
  6.     Set Sh(1) = Workbooks.Open(xlPath & "test21.csv").Sheets(1)
  7.     Set Sh(2) = Sh(1).Parent.Sheets.Add
  8.     Sh(1).Cells.Copy Sh(2).Cells(1)                                                 '複製 test21.csv 的資料                          '
  9.     xlCsv = Dir(xlPath & "*.Csv")                                                   '尋找 *.Csv檔案
  10.     Do While xlCsv <> "" And LCase(xlCsv) <> "test21.csv"
  11.      With Workbooks.Open(xlPath & xlCsv).Sheets(1)
  12.            Sh(2).Cells(Sh(2).Rows.Count, 1).End(xlUp).Offset(2) = "[*" & xlCsv & "*]"
  13.            .[a1].CurrentRegion.Copy Sh(2).Cells(Sh(2).Rows.Count, 1).End(xlUp).Offset(1)  '複製 *.Csv的資料
  14.            .Parent.Close 0
  15.      End With
  16.      xlCsv = Dir
  17.     Loop
  18.     Sh(1).Cells.Clear     'test21.csv.Sheets(1) 清除所有資料: 已備重新匯入排序後的*.Csv
  19.     '*** 處裡 已匯入的 *.Csv*********
  20.     With Sh(2)
  21.         .Activate
  22.         Ar = .Range("a:a").Value
  23.         .Range("a:a").Replace "[*.*]", "=1/0"                                       '[*.Csv] 替代為錯誤值
  24.         .Range("a:a").SpecialCells(xlCellTypeFormulas, xlErrors).Name = "檔名"      '將有錯誤值的儲存格 定義名稱
  25.         .Range("a:a").Value = Ar                                                    '複原原來的值
  26.         With .Columns(Columns.Count)
  27.             [檔名].Copy .Cells(1)
  28.             .Sort Key1:=.Range("a1"), Order1:=xlAscending, Header:=xlGuess          '排序[檔名]
  29.             xR = 1
  30.             Do While .Cells(xR) <> ""                                               '匯入 "檔名"資料
  31.                 xF = Application.Match(.Cells(xR), .Parent.Columns(1), 0)           '尋找 "檔名"
  32.                 xi = Sh(1).Cells(Sh(1).Rows.Count, 1).End(xlUp).Row
  33.                 xi = IIf(xi = 1, 1, xi + 2)                                         '第二個[*.Csv]以後 須再往下位移到2列
  34.                 .Parent.Cells(xF, 1).CurrentRegion.Copy Sh(1).Cells(xi, 1)
  35.                 xi = Sh(1).Cells(Sh(1).Rows.Count, 1).End(xlUp).Row
  36.                 Sh(1).Cells(xi + 2, 1) = "[*div*]"
  37.                 xR = xR + 1
  38.             Loop
  39.         End With
  40.         Application.DisplayAlerts = False
  41.         .Delete                                      '刪除工作表
  42.         Application.DisplayAlerts = True
  43.     End With
  44.     '*****測試 成功後 解除註解 可存檔
  45.     'Sh(1).Parent.Close True
  46. End Sub
複製代碼

TOP

回復 7# luke
版本的問題
6#程式碼以更新 試試看

TOP

本帖最後由 GBKEE 於 2012-6-6 07:03 編輯

回復 10# luke
7# 的問題 是2003 以上的版本 使用End 屬性,如有計算  Rows.Count  或 Columns.Count 須指明它的父層物件
Sh(1).Rows.Count  或 Sh(1).Columns.Count
10# 的問題 修正用Find 取代 Match 找到 真正的字串 試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Sh(1 To 2) As Worksheet, Ar, E As Variant, xlCsv As String, xlPath As String
  4.      Dim xi As Integer, xR As Integer, xF As Range, xlRowsCount As Long
  5.     xlRowsCount = ActiveSheet.Rows.Count
  6.     xlPath = ThisWorkbook.Path & "\"                                                '->修改為正確的檔案路徑
  7.     Set Sh(1) = Workbooks.Open(xlPath & "test21.csv").Sheets(1)
  8.     Set Sh(2) = Sh(1).Parent.Sheets.Add                                             '新增工作表作為 資料暫存
  9.     Sh(1).Cells.Copy Sh(2).Cells(1)                                                 '複製 test21.csv 的資料                          '
  10.     xlCsv = Dir(xlPath & "*.Csv")                                                   '尋找 *.Csv檔案
  11.     Do While xlCsv <> "" And LCase(xlCsv) <> "test21.csv"
  12.      With Workbooks.Open(xlPath & xlCsv).Sheets(1)
  13.            Sh(2).Cells(xlRowsCount, 1).End(xlUp).Offset(2) = "[*" & xlCsv & "*]"
  14.            .[a1].CurrentRegion.Copy Sh(2).Cells(xlRowsCount, 1).End(xlUp).Offset(1)  '複製 *.Csv的資料
  15.            .Parent.Close 0
  16.      End With
  17.      xlCsv = Dir
  18.     Loop
  19.     Sh(1).Cells.Clear     'test21.csv.Sheets(1) 清除所有資料: 已備重新匯入排序後的*.Csv
  20.     '*** 處裡 已匯入的 *.Csv*********
  21.     With Sh(2)
  22.         .Activate
  23.         Ar = .Range("a:a").Value
  24.         .Range("a:a").Replace "[*.*]", "=1/0"                                       '[*.Csv] 替代為錯誤值
  25.         .Range("a:a").SpecialCells(xlCellTypeFormulas, xlErrors).Name = "檔名"      '將有錯誤值的儲存格 定義名稱
  26.         .Range("a:a").Value = Ar                                                    '複原原來的值
  27.         With .Columns(Columns.Count)
  28.             [檔名].Copy .Cells(1)
  29.             .Sort Key1:=.Range("a1"), Order1:=xlAscending, Header:=xlNo             '排序[檔名]
  30.             xR = 1
  31.             Do While .Cells(xR) <> ""                                               '匯入 "檔名"資料
  32.                Set xF = .Parent.Columns(1).Find(.Cells(xR).Text, LookAT:=xlWhole)   '尋找 "檔名"
  33.                 xi = Sh(1).Cells(xlRowsCount, 1).End(xlUp).Row
  34.                 xi = IIf(xi = 1, 1, xi + 2)                                         '第二個[*.Csv]以後 須再往下位移到2列
  35.                 xF.CurrentRegion.Copy Sh(1).Cells(xi, 1)
  36.                 xi = Sh(1).Cells(xlRowsCount, 1).End(xlUp).Row
  37.                 Sh(1).Cells(xi + 2, 1) = "[*div*]"
  38.                 xR = xR + 1
  39.             Loop
  40.         End With
  41.         Application.DisplayAlerts = False
  42.         .Delete                                                                      '刪除資料暫存工作表
  43.         Application.DisplayAlerts = True
  44.     End With
  45.     '*****測試 成功後 解除註解 可存檔
  46.     'Sh(1).Parent.Close True
  47. End Sub
複製代碼

TOP

回復 14# luke
排序情形變得很錯亂
可以看看嗎?

TOP

回復 17# luke
Sh(1).Parent.Close True
Sh(1).Parent.SaveAs Filename:="D:\TEST21OK.txt", FileFormat:=xlUnicodeText, CreateBackup:=False

ActiveWorkbook.SaveAs fd & "結果\" & "TEST21OK.csv", 6
ActiveWorkbook.SaveAs  "D:\TEST21OK.txt", FileFormat:=xlUnicodeText, CreateBackup:=False

TOP

回復 20# luke
  1. Option Explicit
  2. Sub 刪檔名()
  3.     Dim xi As Integer
  4.     With Workbooks("TEST21OK.CSV").Sheets(1)
  5.         .[A:A].Replace "[*]", "=1/0"
  6.         .Range("A:A").SpecialCells(xlCellTypeFormulas, xlErrors).Name = "xx"
  7.         .Range("xx").EntireRow.Delete
  8.         .Range("a:a").SpecialCells(xlCellTypeBlanks).Name = "xx"
  9.         For xi = 1 To .Range("xx").Areas.Count - 1
  10.             .Range("xx").Areas(xi).Cells(1).EntireRow.Delete
  11.          Next
  12.     End With
  13. End Sub
複製代碼

TOP

回復 22# luke
  1. Option Explicit
  2. Sub 拆檔()
  3.     Dim Ar(), MyPath As String, E As Range, Rng As Range, xlFileName As String
  4.     Dim ArFile(), Msg As String
  5.      'CurDir  傳回一個 Variant (String),用來代表目前的路徑。
  6.     MyPath = CurDir & "\"  '自行修改正確路徑。
  7.     If Dir(MyPath & "*.csv") <> "" Then Kill MyPath & "*.csv"   '刪除目前的路徑下的.cgs 檔案
  8.     With Workbooks("TEST21.csv").Sheets(1)
  9.         Ar = .Range("a:a").Value
  10.         .Range("a:a").Replace "[*.*]", "=1/0"                                       '[*.*] 替代為錯誤值
  11.         .Range("a:a").SpecialCells(xlCellTypeFormulas, xlErrors).Name = "xx"        '定義名稱: 錯誤值的儲存格
  12.         .Range("a:a").Value = Ar
  13.         For Each E In .[XX]
  14.             Set Rng = E.CurrentRegion
  15.             Set Rng = .Range(E.Cells(2, 1), Rng.Cells(Rng.Rows.Count, Rng.Columns.Count))
  16.             xlFileName = Replace(Replace(E, "[*", ""), "*]", "")
  17.             With Workbooks.Add(1)
  18.                 Rng.Copy .Sheets(1).[a1]
  19.                 .SaveAs MyPath & xlFileName, xlUnicodeText
  20.                 .Close 0
  21.             End With
  22.          Next
  23.          .Parent.Close 0
  24.     End With
  25. End Sub
複製代碼

TOP

        靜思自在 : 對父母要知恩,感恩、報恩。
返回列表 上一主題