標題:
[發問]
文字檔以覆蓋或貼上方式匯入EXCEL指定欄位
[打印本頁]
作者:
imp
時間:
2014-3-26 16:43
標題:
文字檔以覆蓋或貼上方式匯入EXCEL指定欄位
請問要將文字檔內容匯入至指定欄位中,因指定欄位皆有對應計算公式,故需要以貼上或覆蓋的方式匯入而不是以插入方式匯入資料(這樣後續計算公式會亂掉),請問下面內容要如何修改才能達成呢??
Sub OpenFile()
Dim strFilt As String
Dim strTitle As String
Dim strFname As Variant
Dim i As Integer
Dim strMsg As String
strFilt = "文字檔案,*.txt,"
strTitle = "打開Excel文件"
strFname = Application.GetOpenFilename(FileFilter:=strFilt, Title:=strTitle, MultiSelect:=True)
If Not IsArray(strFname) Then
MsgBox "沒選擇文件!"
Else
For i = LBound(strFname) To UBound(strFname)
strMsg = strMsg & strFname(i) & vbCrLf
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & strFname(i), Destination:=Range("$S$2"))
.Name = "18"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 950
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1)
.TextFileFixedColumnWidths = Array(14)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
Columns("S:AG").Select
Range("S2").Activate
Selection.ColumnWidth = 3
End With
Next
MsgBox "選擇的文件是:" & vbCrLf & strMsg
End If
End Sub
複製代碼
作者:
huijuang
時間:
2014-3-28 10:51
Sub test()
'開啟test.txt的檔案
Open "c:\temp\test.txt" For Input As #1
i = 1
Do While Not EOF(1)
Input #1, a, b '讀取test.txt的資料,註:這裡的資料每行只有2個資料,所以是只有a,b
Range("a1").Offset(i - 1, 0) = a '將資料放到a1開始以下
Range("b1").Offset(i - 1, 0) = b '將資料放到b1開始以下
i = i + 1
Loop
Q:
Close #1 '把test.txt關掉,這裡沒關的話,之後開啟會出現錯誤
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)