- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
本帖最後由 GBKEE 於 2017-9-9 09:33 編輯
回復 4# vm3503ru8
.CommandType = 0 他都說呼叫不正確
沒看到程式,不了解- Option Explicit
- Dim xPath As String
- Sub Ex()
- xPath = "d:\" 'ThisWorkbook.Path
- Ex_修改最新文字檔
- Workbooks.Open Latest_file(xPath) '匯入TXT到EXCEL
- End Sub
- Sub Ex_修改最新文字檔()
- Dim xFile As String, F As Object, i As Integer
- Dim My(), myRow As Integer
- xFile = Latest_file(xPath) 'Latest_file函數(Function) ,傳回:最新存檔的檔案名稱
- Open xFile For Input As #1
- Do Until EOF(1)
- ReDim Preserve My(0 To myRow)
- Input #1, My(myRow) ', my(1), my(2), my(3), my(4), my(5), my(6)
- myRow = myRow + 1
- Loop
- Close #1
- Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile(xFile, True) '開啟文件檔,可覆蓋原文件檔模式
- For i = 0 To UBound(My)
- F.Writeline My(i)
- Next
- F.Close
- End Sub
- Function Latest_file(資料夾路徑 As String) As String '自訂函數(Function) ,傳回:最新存檔的檔案名稱
- Dim F As Object, E, AR(), i As Integer, A As Variant, xFile As String
- Set F = CreateObject("Scripting.FileSystemObject").GetFolder(資料夾路徑).Files
- ReDim AR(1 To 2, 1 To F.Count)
- For Each E In F
- If E Like "*.txt" Then '檔案副檔名為txt
- i = i + 1
- AR(1, i) = E '陣列第一維 置入檔案名稱
- AR(2, i) = CDbl(E.DateLastModified) '陣列第二維 置入存檔時間
- End If
- Next
- A = Application.WorksheetFunction.Index(AR, 2)
- On Error Resume Next
- Latest_file = AR(Application.Match(Application.Max(A), A, 0), 1)
- If Err Then MsgBox 資料夾路徑 & "資料夾,找不到 txt檔案 !!! ": End
- End Function
複製代碼 |
|