返回列表 上一主題 發帖

請問如何將選取的txt檔內容匯入指定儲存格呢?

請問如何將選取的txt檔內容匯入指定儲存格呢?

[版主管理留言]
  • Hsieh(2013-4-23 23:33): 請把txt檔與希望得到結果檔案附上

本帖最後由 Mindyj 於 2013-4-23 14:48 編輯

請教各位前輩,我目前爬文找到將txt檔以excel模式開啟

可是尚未找到任何可以直接讀取和複製txt檔內容貼上至指定sheet1儲存格A6,並且固定儲存格大小

可以請教各位前到該如何執行這個部份嗎? 謝謝

[attach]14754

爬文的內容如下
Dim fileToOpen
     fileToOpen = Application.GetOpenFilename(FileFilter:="Text Files (*.txt),*.txt", Title:="please select the file")
     If fileToOpen <> False Then
        Workbooks.OpenText Filename:=fileToOpen _
         , origin:=950, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
         xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _
         Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
         Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), _
         TrailingMinusNumbers:=True
     End If

Import txt.zip (16.38 KB)

import

Import txt.zip (16.38 KB)

補上txt檔與希望結果

Import.zip (20.25 KB)
回復 1# Mindyj

TOP

小女不才,不知如何更改此碼,問題如下
問題1. txt檔會隨著使用者所選擇的檔案而有所不同,所以才會使用macro進行檔案選取的動作
問題2. 使用者在選取檔案後,macro會將選取檔案之內容複製並貼上至指定sheet與指定儲存格中

TOP

感謝版主、測試成功!!!   

TOP

請問版主、如果選擇的檔案型態不限制、並將貼上欄位固定在A1該怎麼修改呢?
我試過將檔案類型改為all,可是並不適用。
  1. Dim A As Range
  2. fs = Application.GetOpenFilename("Files (*.all), *.all")
  3. Set A = Application.InputBox("Choose the file position", , "$A$1", , , , , 8)
  4. Open fs For Input As #1
  5. Do While Not EOF(1)    ' Loop to the end of the file?
  6.     Line Input #1, TextLine
  7.     A.Offset(i, 0) = TextLine
  8.     i = i + 1
  9. Loop
  10. Close #1
複製代碼
回復 5# Hsieh

TOP

回復 8# luhpro

謝謝luhpro,我學習到實用的一課。

不過現在延伸出另一個問題,要怎麼樣才能讓任何檔案都轉為html的方式貼到excel上面呢?

TOP

        靜思自在 : 【時間無法遮擋】怕時間消逝,花了許多心血,想盡各式方法要遮擋時間,結果是:浪費了更多時間,且一無所成!
返回列表 上一主題