- 帖子
- 4901
- 主題
- 44
- 精華
- 24
- 積分
- 4916
- 點名
- 142
- 作業系統
- Windows 7
- 軟體版本
- Office 20xx
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台北
- 註冊時間
- 2010-4-30
- 最後登錄
- 2026-7-16
                
|
2#
發表於 2011-12-27 08:43
| 只看該作者
回復 1# v60i
用資料夾對話方塊結果取代ThisWorkbook.Path- Sub 匯入文字檔()
- Dim xFile, uFile, uHead As Range, Jm&, Km&, X, xT, xL
- Range("A:A").Clear '清除舊匯入資料
- '-----------------------------------------------------
- With Application.FileDialog(msoFileDialogFolderPicker)
- .Show
- fd = .SelectedItems(1)
- End With
- Application.ScreenUpdating = False
- Do
- If xChk = 0 Then
- xFile = Dir(fd & "\*.txt")
- If xFile = "" Then MsgBox "※找不到 TXT 檔案! ", 0 + 16: Exit Sub
- xChk = 1
- Else
- xFile = Dir
- If xFile = "" Then Exit Do
- End If
- '----------------------------------------------
- uFile = fd & "\" & xFile
- Set uHead = Range("A65536").End(xlUp)
- If uHead <> "" Then Set uHead = uHead(3, 1)
- With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & uFile, Destination:=uHead)
- .AdjustColumnWidth = False
- .TextFileColumnDataTypes = Array(1)
- .Refresh BackgroundQuery:=False
- .Delete
- End With
- uHead.Interior.ColorIndex = 6
- '每筆第一格加〔黃色〕底
- NEXT_LINE:
- Loop
- '-------------------------------------------------------
- Application.ScreenUpdating = True
- MsgBox "∼∼匯入完成∼∼ "
- End Sub
複製代碼 |
|