在新工作表中将文本文件浏览到活动工作簿中

时间:2013-05-29 13:21:23

标签: excel-vba vba excel

我想将文本文件浏览到当前工作簿中的位置A1。我记录了相同的宏,但它不允许我浏览文本文件,因此我做了一些更改。但是当我运行宏时,我得到一个运行时错误13,说明类型不匹配。我已经包含了以下代码

Sub Button2_Click()

    Filename = Application.GetOpenFilename(FileFilter:="text files (*.txt*), *.txt*", Title:="Please select a file")
    If Filename = False Then
        MsgBox ("wrong file browsed. please retry")
        Exit Sub

    Else

        With ThisWorkbook.Sheets(Sheet1).QueryTables.Add(Connection:= _
            "TEXT;" & fStr, Destination:=ThisWorkbook.Sheets(Sheet1).Range("$A$1"))
            .Name = "C24831300-2"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Columns("A:L").Select
        Range("L1").Activate
        Selection.Delete Shift:=xlToLeft
        Columns("E:BB").Select
        Selection.Delete Shift:=xlToLeft
        Columns("B:B").Select
        Selection.Delete Shift:=xlToLeft
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        ActiveWindow.SmallScroll Down:=-6
        Columns("A:A").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
            :="_", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
            TrailingMinusNumbers:=True
        Range("D2").Select
        ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-2],""-"",RC[2])"
        Range("D2").Select
        Selection.AutoFill Destination:=Range("D2:D419")
        Range("D2:D419").Select
        Sheets("Project Tracker").Select
        ActiveWindow.SmallScroll Down:=-6
        Range("H5").Select
        ActiveCell.FormulaR1C1 = _
            "=VLOOKUP(CONCATENATE(RC3,""-"",R4C),Sheet1!C4:C5,2,FALSE)"
        Range("H5").Select
        Selection.AutoFill Destination:=Range("H5:H122")
        Range("H5:H122").Select
        Range("H5").Select
        Selection.AutoFill Destination:=Range("H5:I5"), Type:=xlFillDefault
        Range("H5:I5").Select
        Range("I5").Select
        Selection.AutoFill Destination:=Range("I5:I122")
        Range("I5:I122").Select

        Range("H5:I122").Select
        Selection.Copy
        Range("H5").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("H:I").Select
        Selection.Replace What:="#N/A", Replacement:="N/A", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

        Columns("H:I").Select
        Selection.Replace What:="BON POUR INFO", Replacement:="BPI", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

    End If
End Sub

1 个答案:

答案 0 :(得分:1)

错误在

With ThisWorkbook.Sheets(Sheet1)

应该是

With ThisWorkbook.Sheets("Sheet1")