返回列表 上一主題 發帖

[發問] 請問如何更改VB的連結檔案路徑

回復 1# v60i
用資料夾對話方塊結果取代ThisWorkbook.Path
  1. Sub 匯入文字檔()
  2. Dim xFile, uFile, uHead As Range, Jm&, Km&, X, xT, xL
  3. Range("A:A").Clear '清除舊匯入資料
  4. '-----------------------------------------------------
  5. With Application.FileDialog(msoFileDialogFolderPicker)
  6. .Show
  7. fd = .SelectedItems(1)
  8. End With

  9. Application.ScreenUpdating = False
  10. Do
  11.     If xChk = 0 Then
  12.        xFile = Dir(fd & "\*.txt")
  13.        If xFile = "" Then MsgBox "※找不到 TXT 檔案! ", 0 + 16: Exit Sub
  14.        xChk = 1
  15.     Else
  16.        xFile = Dir
  17.        If xFile = "" Then Exit Do
  18.     End If
  19.     '----------------------------------------------
  20.     uFile = fd & "\" & xFile
  21.     Set uHead = Range("A65536").End(xlUp)
  22.     If uHead <> "" Then Set uHead = uHead(3, 1)
  23.     With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & uFile, Destination:=uHead)
  24.          .AdjustColumnWidth = False
  25.          .TextFileColumnDataTypes = Array(1)
  26.          .Refresh BackgroundQuery:=False
  27.          .Delete
  28.     End With
  29.     uHead.Interior.ColorIndex = 6
  30.     '每筆第一格加〔黃色〕底
  31. NEXT_LINE:
  32. Loop
  33. '-------------------------------------------------------
  34. Application.ScreenUpdating = True
  35. MsgBox "∼∼匯入完成∼∼ "
  36. End Sub
複製代碼
學海無涯_不恥下問

TOP

        靜思自在 : 受人點水之恩,須當湧泉以報。
返回列表 上一主題