PC Review


Reply
Thread Tools Rate Thread

Clearing ".csv" file using VBA

 
 
Mike
Guest
Posts: n/a
 
      12th Dec 2011
Hi everyone,
Say I have this VBA piece:

Set BRngSolution =
Workbooks(filename).Worksheets("TimeMacTable").Range("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?

Thanks,
Mike



 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      12th Dec 2011
If you mean to clearcontents of the 1st page of the file then recorded/
edited
Sub Macro3()
Workbooks.Open Filename:="C:\yourfoledername\Book2.csv"
Cells.ClearContents
Activeworkbook.Close SaveChanges:=False
End Sub
Or do you want to KILL it.


On Dec 12, 9:24*am, Mike <mas_it_2...@yahoo.com> wrote:
> Hi everyone,
> Say I have this VBA piece:
>
> Set BRngSolution =
> Workbooks(filename).Worksheets("TimeMacTable").Range("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?
>
> Thanks,
> Mike


 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      12th Dec 2011
On Dec 12, 11:05*am, Don Guillett <dguille...@gmail.com> wrote:
> If you mean to clearcontents of the 1st page of the file then recorded/
> edited
> Sub Macro3()
> * * Workbooks.Open Filename:="C:\yourfoledername\Book2.csv"
> * * Cells.ClearContents
> * * Activeworkbook.Close SaveChanges:=False
> End Sub
> Or do you want to KILL it.
>
> On Dec 12, 9:24*am, Mike <mas_it_2...@yahoo.com> wrote:
>
>
>
> > Hi everyone,
> > Say I have this VBA piece:

>
> > Set BRngSolution =
> > Workbooks(filename).Worksheets("TimeMacTable").Range("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?

>
> > Thanks,
> > Mike- Hide quoted text -

>
> - Show quoted text -


Just clearing not removing. Thanks
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      12th Dec 2011
On Dec 12, 11:05*am, Don Guillett <dguille...@gmail.com> wrote:
> If you mean to clearcontents of the 1st page of the file then recorded/
> edited
> Sub Macro3()
> * * Workbooks.Open Filename:="C:\yourfoledername\Book2.csv"
> * * Cells.ClearContents
> * * Activeworkbook.Close SaveChanges:=False
> End Sub
> Or do you want to KILL it.
>
> On Dec 12, 9:24*am, Mike <mas_it_2...@yahoo.com> wrote:
>
>
>
> > Hi everyone,
> > Say I have this VBA piece:

>
> > Set BRngSolution =
> > Workbooks(filename).Worksheets("TimeMacTable").Range("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?

>
> > Thanks,
> > Mike- Hide quoted text -

>
> - Show quoted text -


But if I have many ".csv" files, do I need to write this for every
file?
 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      12th Dec 2011
Mike brought next idea :
> On Dec 12, 11:05*am, Don Guillett <dguille...@gmail.com> wrote:
>> If you mean to clearcontents of the 1st page of the file then recorded/
>> edited
>> Sub Macro3()
>> * * Workbooks.Open Filename:="C:\yourfoledername\Book2.csv"
>> * * Cells.ClearContents
>> * * Activeworkbook.Close SaveChanges:=False
>> End Sub
>> Or do you want to KILL it.
>>
>> On Dec 12, 9:24*am, Mike <mas_it_2...@yahoo.com> wrote:
>>
>>
>>
>>> Hi everyone,
>>> Say I have this VBA piece:

>>
>>> Set BRngSolution =
>>> Workbooks(filename).Worksheets("TimeMacTable").Range("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?

>>
>>> Thanks,
>>> Mike- Hide quoted text -

>>
>> - Show quoted text -

>
> But if I have many ".csv" files, do I need to write this for every
> file?


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...

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:

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

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
Harlan Grove
Guest
Posts: n/a
 
      19th Dec 2011
On Dec 12, 7:24*am, Mike <mas_it_2...@yahoo.com> wrote...
....
>Set BRngSolution =
>Workbooks(filename).Worksheets("TimeMacTable").Range("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"

....

You want to clear only the value in the 6th line, 2nd field in the CSV
file?

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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:04 PM.