Results 1 to 4 of 4

Thread: how to clear .csv file in vba?

  1. #1
    Join Date
    Nov 2011
    Posts
    61

    how to clear .csv file in vba?

    Hi everyone,
    Say I have this VBA piece:

    Code:
    Set BRngSolution =
    Workbooks(filename).Worksheets("TimeMacTable").Ran ge("B6")
    BRngSolution.Clear
    but instead of a range in Excel sheet, I have thing in "abc.csv" that I want to Clear. I tried this but didn't work:

    Clear "C:\AM\Model\abc.csv" how it should be written in VBA?

  2. #2
    Join Date
    Nov 2010
    Posts
    503

    Re: how to clear .csv file in vba?

    If you mean to clearcontents of the 1st page of the file then recorded or edited.
    Code:
    Sub Macro3()
    Workbooks.Open Filename:="C:\yourfoledername\Book2.csv"
    Cells.ClearContents
    Activeworkbook.Close SaveChanges:=False
    End Sub

  3. #3
    Join Date
    Dec 2010
    Posts
    351

    Re: how to clear .csv file in vba?

    One way to clear the contents of a folder full of CSV files is to use standard VB I/O and overwrite them by looping through the folder and rewriting them with an empty string. Here's a reusable procedure to do
    this...

    Code:
    Sub WriteTextFileContents(Text As String, Filename As String, Optional
    AppendMode As Boolean = False)
    ' A reuseable procedure to write, overwrite, or append large amounts of
    data
    ' to a text file in one single step.
    Dim iNum As Integer
    On Error GoTo ErrHandler
    iNum = FreeFile()
    If AppendMode Then
    Open Filename For Append As #iNum: Print #iNum, vbCrLf & Text;
    Else
    Open Filename For Output As #iNum: Print #iNum, Text;
    End If
    
    ErrHandler:
    Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
    End Sub 'WriteTextFileContents()
    ...and here's how to use it:
    Code:
    Sub OverWriteCSVs()
    Const sText As String = ""
    Dim f As Variant, sPath As String
    
    With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = False Then Exit Sub 'User cancels
    sPath = .SelectedItems(1)
    End With
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    
    f = Dir(sPath, 7)
    Do While f <> ""
    If UCase(Right(f, 3)) = "CSV" Then _
    WriteTextFileContents sText, sPath & f
    f = Dir 'Get next file
    Loop
    End Sub

  4. #4
    Join Date
    Mar 2011
    Posts
    542

    Re: how to clear .csv file in vba?

    Code:
    Sub foo()
    Dim f As Workbook
    
    Application.DisplayAlerts = False
    Set f = Workbooks.Open(Filename:="C:\AM\Model\abc.csv", Format:=2)
    f.Worksheets(1).Range("B6").ClearContents
    f.Close SaveChanges:=True
    
    End Sub

Similar Threads

  1. How to clear file permission in Windows 7
    By Asheshs in forum Operating Systems
    Replies: 4
    Last Post: 29-03-2013, 04:11 PM
  2. How to clear navigation with Quattro File Explorer?
    By ChaluCh in forum Windows Software
    Replies: 4
    Last Post: 23-02-2011, 12:50 PM
  3. How to clear Bad File Descriptor error
    By InterMania in forum Operating Systems
    Replies: 3
    Last Post: 14-10-2009, 10:48 PM
  4. Replies: 2
    Last Post: 04-02-2009, 10:58 PM
  5. Clear Page file on shutdown in Windows Xp
    By Conrad in forum Tips & Tweaks
    Replies: 0
    Last Post: 18-11-2008, 05:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,638,435,678.91525 seconds with 17 queries