2013-12-19 247 views
0

我試圖從TXT文件導入數據到我的Excel工作表。將txt文件導入到excel中; QueryTables

它適用於低於VBA宏,但唯一的問題是重新打開Excel文件宏後試圖尋找txt目錄,當它無法找到它時,它是給出錯誤。

我不打算在那裏放置這樣的命令,但現在我不知道如何禁用它。你們有關於我應該改變什麼來禁用該功能的想法嗎?

Sub test_9() 
Dim jess916 As Variant, FullPath As String 
Set jess916 = Application.FileDialog(msoFileDialogFilePicker) 
With jess916 
    .InitialView = msoFileDialogViewDetails 
    .InitialFileName = ThisWorkbook.Path 
    .Filters.Add "Open File ", "*.txt", 1 
    .ButtonName = "Import file" 
    .Title = " jess916c Search for .txt file to Import" 
    If .Show = -1 Then 
     FullPath = .SelectedItems(1) 
Else: 
     Exit Sub 
    End If 
End With 
With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;" & FullPath, Destination:=Range("A2")) 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = True 
    .RefreshStyle = xlOverwriteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = False 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 850 
    .TextFileStartRow = 9 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = True 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = False 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 
End Sub 
+0

你的意思是說你關閉了你的xlsx文件,你再次打開它,並且VBA宏自動運行? –

回答

0

我使用On Error Goto來處理我的代碼。見下文。

Sub ImportData() 
    Application.ScreenUpdating = False 
    Dim intChoice As Integer 
    Dim strPath As String 

    'only allow the user to select one file 
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False 
    'make the file dialog visible to the user 
    intChoice = Application.FileDialog(msoFileDialogOpen).Show 
    'determine what choice the user made 
    If intChoice <> 0 Then 
     'get the file path selected by the user 
     strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1) 
    End If 
    'Import data from file 
    On Error GoTo errorHandler 
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & strPath, Destination:=Range("$A$1")) 
     .Name = "MemoQ Data Range" 
     .FieldNames = True 
     .RowNumbers = False 
     .FillAdjacentFormulas = False 
     .PreserveFormatting = True 
     .RefreshOnFileOpen = False 
     .RefreshStyle = xlInsertDeleteCells 
     .SavePassword = False 
     .SaveData = True 
     .AdjustColumnWidth = True 
     .RefreshPeriod = 0 
     .TextFilePromptOnRefresh = False 
     .TextFilePlatform = 1252 
     .TextFileStartRow = 1 
     .TextFileParseType = xlDelimited 
     .TextFileTextQualifier = xlTextQualifierDoubleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = True 
     .TextFileCommaDelimiter = False 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
    End With 

    Application.ScreenUpdating = True 
    Exit Sub 

errorHandler: 
    Exit Sub 
End Sub