简体   繁体   中英

To Browse a text file into the active workbook in a new sheet

I want to browse a text file to a location A1 in the current workbook. I recorded the a macro for the same but it wouldnt allow me to browse a text file, hence i made a few changes. but when i run the macro i get a runtime error 13 stating type mismatch. i have included the code below

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

The error is in

With ThisWorkbook.Sheets(Sheet1)

It should be

With ThisWorkbook.Sheets("Sheet1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM