XDSSIOP
XDSSIOP

Reputation: 91

How to delete the content of a txt file with vba?

I would like to make transactions with SAP, I use a csv file as an exported file, but in the end I would like to delete the content of this file without deleting the file itself. Just delete the content.

      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

Upvotes: 3

Views: 8626

Answers (1)

jun
jun

Reputation: 592

Sub ClearContents()

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

End Sub

Just change the file path to where your csv file is.

Upvotes: 9

Related Questions