|
すみません、さきほど抜書きしてしまったのですが、あれだけだとやはりエラーになります。
カレントパスの取得と設定、およびシートのアクティブ化をしないと正常に動きません。
再現性のあるもの(多分・・)を上げておきます。
Sheet1とSheet2の内容はそれぞれ下記の通りです。
'-----------------------------------------------------------------------------------
'--------------------------------------Sheet1---------------------------------------
'-----------------------------------------------------------------------------------
Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" ( _
ByVal CurrentDir As String) As Long
Private Sub SetCurDir()
SetCurrentDirectory (ThisWorkbook.Path)
End Sub
Sub ImportData()
Worksheets("Sheet2").Activate
Call ActiveSheet.QT
End Sub
Private Sub CommandButton1_Click()
Call SetCurDir
Call ImportData
End Sub
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
'--------------------------------------Sheet2---------------------------------------
'-----------------------------------------------------------------------------------
Sub QT()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;baz.dat", Destination:=Range("A1"))
' .Name = "Sheet2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 1)
.TextFileFixedColumnWidths = Array(24)
.Refresh BackgroundQuery:=False
End With
End Sub
'-----------------------------------------------------------------------------------
|
|