如何用vba删除txt文件的内容?

XDS*_*IOP 3 excel vba

我想与 SAP 进行交易,我使用 csv 文件作为导出文件,但最后我想删除此文件的内容而不删除文件本身。删除内容就行了。

      Sub OpenCSVFile()
       '
       ' Load the CSV extract
       '

       '
     With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & fpath & "\" & ffilename, Destination:=Range("$A$1"))
    .Name = "text"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 850
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileOtherDelimiter = "|"
    .TextFileColumnDataTypes = Array(1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False

     End With


     With ActiveSheet
    .Columns(1).EntireColumn.Delete 'delete first column
    .Rows("1:7").EntireRow.Delete 'delete first 7 rows
     End With

     End Sub
Run Code Online (Sandbox Code Playgroud)

jun*_*jun 6

Sub ClearContents()

    Open "C:\Users\Username\Desktop\test1.csv" For Output As #1: Close #1
    MsgBox "Clear complete"

End Sub
Run Code Online (Sandbox Code Playgroud)

只需将文件路径更改为您的 csv 文件所在的位置。