Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / December 2007

Tip: Looking for answers? Try searching our database.

excel to CSV

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ram - 13 Dec 2007 18:03 GMT
I have VBA macro that converts excel to csv. It also removes hidden
characters. But if a cell start with "=" then it gives an error (I used clean
in the VBA). I know that = is for entering formulas. But I enter in the cell
as '= sfdsdfdsf. This way I can put = as the first letter in the cell. Does
anyone know how to do get rid of this error
Joel - 13 Dec 2007 18:45 GMT
try this code.  modify path and file name as necessary

Sub WriteCSV()

Const Delimiter = ","

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const MyPath = "C:\temp\"
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
   

Set fswrite = CreateObject("Scripting.FileSystemObject")
   
WriteFileName = "text.csv"

  'open files
  WritePathName = MyPath + WriteFileName
  fswrite.CreateTextFile WritePathName
  Set fwrite = fswrite.GetFile(WritePathName)
  Set tswrite = fwrite.OpenAsTextStream(ForWriting, TristateUseDefault)

  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
 
  For RowCount = 1 To LastRow
     LastCol = Cells(RowCount, Columns.Count).End(xlToLeft).Column
     For ColCount = 1 To LastCol
        If ColCount = 1 Then
           OutputLine = Cells(RowCount, ColCount)
        Else
           OutputLine = OutputLine & Delimiter & Cells(RowCount, ColCount)
        End If
     Next ColCount
     tswrite.writeline OutputLine
  Next RowCount
     
  tswrite.Close
 
Exit Sub
End Sub

> I have VBA macro that converts excel to csv. It also removes hidden
> characters. But if a cell start with "=" then it gives an error (I used clean
> in the VBA). I know that = is for entering formulas. But I enter in the cell
> as '= sfdsdfdsf. This way I can put = as the first letter in the cell. Does
> anyone know how to do get rid of this error
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.