delete all in a csv file

A

alvin Kuiper

Is there away in access, to delete all in a csv file (excel) I only have one
sheet
 
D

Daniel Pineault

Here is a bit of code I put together rapidly.

Sub ClearXLSWrkSht(sXLSFile As String, sXLSWrkSht As String)
' sXLSFile :: Excel file with full path and extension
' sXLSWrkSht :: Worksheet name to be cleared

Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
' For Early Binding you'd use these 3 lines instead of the 3 above
' Dim xlApp As Excel.Application
' Dim xlBook As Excel.Workbook
' Dim xlSheet As Excel.Worksheet
On Error GoTo Error_Handler

Set xlApp = New Excel.Application
xlApp.Visible = True 'Control whether or not Excel should be visible to
'the user or not.
Set xlBook = xlApp.Workbooks.Open(sXLSFile) 'Open the workbook
Set xlSheet = xlBook.Worksheets(sXLSWrkSht) 'Worksheet we are working with

xlSheet.Cells.Select
xlSheet.Cells.ClearContents 'Clear the contents

xlBook.Close True 'Close and save the workbook
xlApp.Quit 'Close the instance of Excel we create

Error_Handler_Exit:
On Error Resume Next
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Exit Sub

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: ClearXLSWrkSht" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub

It will clear the worksheet, preserving formatting. If you truly wish to
delete everything then you need to change the ' xlSheet.Cells.ClearContents
'Clear the contents' part of the code.
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
J

Jack Leach

A csv file is just a text file... data values seperated by commas. Excel is
just one of a few ways to format the data to a meaningful view. Is there
some reason that this needs to be done in excel? Why not just erase the csv
file?

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top