求助大神呀,下面是一串vba代碼
Sub ImportTXT()
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT; E:\data\pmtest\Sample of TXT pose.TXT“, Destination:=Range("$A$4"))
'.CommandType = 0
.Name = "Sample of TXT pose"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 862
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
想改成相對路徑,以下為代碼
Sub ImportTXT()
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & ThisWorkbook.Path & "\Sample of TXT pose.TXT""", Destination:=Range("$A$4"))
'.CommandType = 0
.Name = "Sample of TXT pose"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 862
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
可是運行會報無法訪問檔案 ‘1004’樣的錯,請大神指點看看哪里出問題了不甚感激。
uj5u.com熱心網友回復:
VB早不用了uj5u.com熱心網友回復:
我也想知道
uj5u.com熱心網友回復:
"TEXT;" & ThisWorkbook.Path & "\Sample of TXT pose.TXT"""--
最后那兩個引號有點多余了,去掉改成
"TEXT;" & ThisWorkbook.Path & "\Sample of TXT pose.TXT"
保存一下,再重新打開EXCEL運行看看。
uj5u.com熱心網友回復:
古董貼...uj5u.com熱心網友回復:
引號沒配對,改了就行轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/72406.html
上一篇:windows7關閉不掉的行程
