返回列表 上一主題 發帖

[發問] VBA 開起最新文字文件檔 並修改

回復 1# vm3503ru8
是最新存檔的日期時間嗎?
  1. Option Explicit
  2. Sub Ex修改文件檔案()
  3.     Dim F As Object, E, AR(), i As Integer, A As Variant, xFile As String
  4.     'Set F = CreateObject("Scripting.FileSystemObject").GetFolder(ThisWorkbook.Path).Files
  5.     Set F = CreateObject("Scripting.FileSystemObject").GetFolder("你的資料夾路徑??").Files
  6.     '.Files ->這資料夾路徑中的所有的檔案
  7.     ReDim AR(1 To 2, 1 To F.Count)
  8.     For Each E In F
  9.         If E Like "*.txt" Then     '檔案副檔名為txt
  10.             i = i + 1
  11.             AR(1, i) = E                        '陣列第一維 置入檔案名稱
  12.             AR(2, i) = CDbl(E.DateLastModified) '陣列第二維 置入存檔的日期時間
  13.         End If
  14.     Next
  15.     A = Application.WorksheetFunction.Index(AR, 2)
  16.     xFile = AR(Application.Match(Application.Max(A), A, 0), 1)  '最新存檔的檔案名稱
  17.     Set F = CreateObject("Scripting.FileSystemObject").OPENTextFile(xFile, 1)   '開啟文件檔,讀取模式
  18.     A = Split(F.readall, vbLf)         '讀取檔案內容
  19.     F.Close                            '關閉文件檔案
  20.     Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile(xFile, True) '開啟文件檔,可覆蓋原文件檔模式
  21.     For i = 0 To UBound(A)
  22.         F.WriteLine IIf(i > 1 And A(i) <> "", Space(1), "") & A(i)
  23.     Next
  24.     F.Close
  25. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2017-9-9 09:33 編輯

回復 4# vm3503ru8
.CommandType = 0 他都說呼叫不正確
沒看到程式,不了解
  1. Option Explicit
  2. Dim xPath As String
  3. Sub Ex()
  4.     xPath = "d:\" 'ThisWorkbook.Path
  5.     Ex_修改最新文字檔
  6.     Workbooks.Open Latest_file(xPath)  '匯入TXT到EXCEL
  7. End Sub
  8. Sub Ex_修改最新文字檔()
  9.     Dim xFile As String, F As Object,  i As Integer
  10.     Dim My(), myRow As Integer
  11.     xFile = Latest_file(xPath)  'Latest_file函數(Function) ,傳回:最新存檔的檔案名稱
  12.     Open xFile For Input As #1
  13.     Do Until EOF(1)
  14.         ReDim Preserve My(0 To myRow)
  15.         Input #1, My(myRow) ', my(1), my(2), my(3), my(4), my(5), my(6)
  16.         myRow = myRow + 1
  17.     Loop
  18.     Close #1
  19.     Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile(xFile, True) '開啟文件檔,可覆蓋原文件檔模式
  20.     For i = 0 To UBound(My)
  21.     F.Writeline My(i)
  22.     Next
  23.     F.Close
  24. End Sub
  25. Function Latest_file(資料夾路徑 As String) As String    '自訂函數(Function) ,傳回:最新存檔的檔案名稱
  26.     Dim F As Object, E, AR(), i As Integer, A As Variant, xFile As String
  27.     Set F = CreateObject("Scripting.FileSystemObject").GetFolder(資料夾路徑).Files
  28.     ReDim AR(1 To 2, 1 To F.Count)
  29.     For Each E In F
  30.         If E Like "*.txt" Then     '檔案副檔名為txt
  31.             i = i + 1
  32.             AR(1, i) = E                        '陣列第一維 置入檔案名稱
  33.             AR(2, i) = CDbl(E.DateLastModified) '陣列第二維 置入存檔時間
  34.         End If
  35.     Next
  36.     A = Application.WorksheetFunction.Index(AR, 2)
  37.     On Error Resume Next
  38.     Latest_file = AR(Application.Match(Application.Max(A), A, 0), 1)
  39.     If Err Then MsgBox 資料夾路徑 & "資料夾,找不到  txt檔案 !!! ": End
  40. End Function
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 11# vm3503ru8
這裡要改
  1. For i = 0 To UBound(A)
  2.          F.WriteLine A(i)
  3.     Next
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 人生不一定球球是好球,但是有歷練的強打者,隨時都可以揮棒。
返回列表 上一主題