ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   excel to CSV (https://www.excelbanter.com/excel-programming/402713-excel-csv.html)

Ram

excel to CSV
 
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

excel to CSV
 
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


"Ram" wrote:

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



All times are GMT +1. The time now is 06:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com