Board logo

標題: 如何每次開啟選擇匯入文字檔之路徑 [打印本頁]

作者: sping    時間: 2017-7-23 09:26     標題: 如何每次開啟選擇匯入文字檔之路徑

以下的程式是用錄製巨集的功能完成的
主要是要將文字檔匯入excel指定欄位
可是因為有時候文字檔路徑不一樣
致程式無法順利執行
不知道要怎樣改成可以跳出視窗選擇檔案路徑,才可以不用每次重錄巨集
希望有人能協助,謝謝
  1. Sub Macro1()
  2. '
  3. ' Macro1 Macro
  4. ' 匯入檔案
  5. '

  6. '
  7.     With ActiveSheet.QueryTables.Add(Connection:= _
  8.         "TEXT;C:\Users\USER\Desktop\新會\99.txt", Destination:=Range("$A$2"))
  9.         .Name = "99"
  10.         .FieldNames = True
  11.         .RowNumbers = False
  12.         .FillAdjacentFormulas = False
  13.         .PreserveFormatting = True
  14.         .RefreshOnFileOpen = False
  15.         .RefreshStyle = xlInsertDeleteCells
  16.         .SavePassword = False
  17.         .SaveData = True
  18.         .AdjustColumnWidth = True
  19.         .RefreshPeriod = 0
  20.         .TextFilePromptOnRefresh = False
  21.         .TextFilePlatform = 950
  22.         .TextFileStartRow = 1
  23.         .TextFileParseType = xlFixedWidth
  24.         .TextFileTextQualifier = xlTextQualifierDoubleQuote
  25.         .TextFileConsecutiveDelimiter = False
  26.         .TextFileTabDelimiter = True
  27.         .TextFileSemicolonDelimiter = False
  28.         .TextFileCommaDelimiter = False
  29.         .TextFileSpaceDelimiter = False
  30.         .TextFileColumnDataTypes = Array(9, 1, 9, 9)
  31.         .TextFileFixedColumnWidths = Array(38, 32, 28)
  32.         .TextFileTrailingMinusNumbers = True
  33.         .Refresh BackgroundQuery:=False
  34.     End With
  35.     ActiveWindow.SmallScroll Down:=-27
  36. End Sub
複製代碼

作者: Kubi    時間: 2017-7-25 10:02

回復 1# sping
試試看
  1. Sub Macro1()
  2. '
  3. ' Macro1 Macro
  4. ' 匯入檔案
  5. '

  6. '
  7.    txtFile = Application.GetOpenFilename("文字檔, *.txt")
  8.    txtFile = "TEXT;" & txtFile
  9.     With ActiveSheet.QueryTables.Add(Connection:=txtFile, Destination:=Range("$A$2"))
  10.         .FieldNames = True
  11.         .RowNumbers = False
  12.         .FillAdjacentFormulas = False
  13.         .PreserveFormatting = True
  14.         .RefreshOnFileOpen = False
  15.         .RefreshStyle = xlInsertDeleteCells
  16.         .SavePassword = False
  17.         .SaveData = True
  18.         .AdjustColumnWidth = True
  19.         .RefreshPeriod = 0
  20.         .TextFilePromptOnRefresh = False
  21.         .TextFilePlatform = 950
  22.         .TextFileStartRow = 1
  23.         .TextFileParseType = xlFixedWidth
  24.         .TextFileTextQualifier = xlTextQualifierDoubleQuote
  25.         .TextFileConsecutiveDelimiter = False
  26.         .TextFileTabDelimiter = True
  27.         .TextFileSemicolonDelimiter = False
  28.         .TextFileCommaDelimiter = False
  29.         .TextFileSpaceDelimiter = False
  30.         .TextFileColumnDataTypes = Array(9, 1, 9, 9)
  31.         .TextFileFixedColumnWidths = Array(38, 32, 28)
  32.         .TextFileTrailingMinusNumbers = True
  33.         .Refresh BackgroundQuery:=False
  34.     End With
  35.     ActiveWindow.SmallScroll Down:=-27
  36. End Sub
複製代碼

作者: PKKO    時間: 2017-7-25 10:48

回復 1# sping

'選檔案   
Set FD = Excel.Application.FileDialog(msoFileDialogFilePicker)  '設定選取檔案功能
FD.Filters.Clear    '清除之前的資料
'FD..InitialFileName = "c:temp\"'默認路徑
FD.Filters.Add "Excel File", "*.xls*" '設定顯示的副檔名
If FD.Show = -1 Then
     Path = FD.SelectedItems(1)
Else
    End
End If
Workbooks.Open Path
作者: sping    時間: 2017-7-26 23:22

回復 2# Kubi


非常謝謝
可以用了
作者: sping    時間: 2017-7-26 23:24

回復 3# PKKO


感謝你
順利解決工作上的問題




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)