Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
CSV File format problem
Your problem seems similar to the one discussed he
http://support.microsoft.com/default...95&Product=xlw You might have to use a macro to get the result you want. Try this one: 'Puts no quotes around strings ''Outputs the selection if more than one cell is selected, else entire sheet ''Include commas for missing items at end of row. Sub OutputActiveSheetAsTrueCSVFileAllCommas() Dim SrcRg As Range Dim CurrRow As Range Dim CurrCell As Range Dim CurrTextStr As String Dim ListSep As String Dim FName As Variant Dim ColCount As Integer Dim CurrCol As Integer FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv") If FName < False Then ListSep = Application.International(xlListSeparator) If Selection.Cells.Count 1 Then Set SrcRg = Selection Else Set SrcRg = ActiveSheet.UsedRange End If ColCount = SrcRg.Columns.Count Open FName For Output As #1 For Each CurrRow In SrcRg.Rows CurrCol = 0 CurrTextStr = "" For Each CurrCell In CurrRow.Cells CurrCol = CurrCol + 1 CurrTextStr = CurrTextStr & CurrCell.Value & IIf(CurrCol < ColCount, ListSep, "") Next Print #1, CurrTextStr Next Close #1 End If End Sub -- Jim Rech Excel MVP "Jac Tremblay" wrote in message ... | Hi everyone, | | I get a problem with a worksheet when I export the data in a csv file. I have a worksheet with 10 rows and 10 columns of data (100 cells). Not all of them are filled. The last cell is J10. | If I export this worksheet to a csv file, I get 9 commas separating the data on each of the 10 lines. That's fine. | If I add data in the range D11:D15, and reexport the worksheet to a csv file, I get 10 lines with 9 commas and 5 lines with 3 commas. That's not what I want. And that's not normal. | | If I use a new worksheet to test my problem, I get 15 lines with 9 commas which is correct. | | What is (or may be) wrong with the first workbook? | | Thanks for your help. | -- | Jac Tremblay |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
CSV File format problem
Hi Jim,
This macro does work well and I thank you for your help. But I still want to understand why one should have to use such a macro when Excel is supposed to do that job automatically. Is it possible that some cell have a text format while the others below do not? Because, when I copy that last cell down to the last row, the problem does not appear and Excel treats these cells as if they had something in them. I guess I will have to live with it... Thank you again. "Jim Rech" wrote: Your problem seems similar to the one discussed he http://support.microsoft.com/default...95&Product=xlw You might have to use a macro to get the result you want. Try this one: 'Puts no quotes around strings ''Outputs the selection if more than one cell is selected, else entire sheet ''Include commas for missing items at end of row. Sub OutputActiveSheetAsTrueCSVFileAllCommas() Dim SrcRg As Range Dim CurrRow As Range Dim CurrCell As Range Dim CurrTextStr As String Dim ListSep As String Dim FName As Variant Dim ColCount As Integer Dim CurrCol As Integer FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv") If FName < False Then ListSep = Application.International(xlListSeparator) If Selection.Cells.Count 1 Then Set SrcRg = Selection Else Set SrcRg = ActiveSheet.UsedRange End If ColCount = SrcRg.Columns.Count Open FName For Output As #1 For Each CurrRow In SrcRg.Rows CurrCol = 0 CurrTextStr = "" For Each CurrCell In CurrRow.Cells CurrCol = CurrCol + 1 CurrTextStr = CurrTextStr & CurrCell.Value & IIf(CurrCol < ColCount, ListSep, "") Next Print #1, CurrTextStr Next Close #1 End If End Sub -- Jim Rech Excel MVP "Jac Tremblay" wrote in message ... | Hi everyone, | | I get a problem with a worksheet when I export the data in a csv file. I have a worksheet with 10 rows and 10 columns of data (100 cells). Not all of them are filled. The last cell is J10. | If I export this worksheet to a csv file, I get 9 commas separating the data on each of the 10 lines. That's fine. | If I add data in the range D11:D15, and reexport the worksheet to a csv file, I get 10 lines with 9 commas and 5 lines with 3 commas. That's not what I want. And that's not normal. | | If I use a new worksheet to test my problem, I get 15 lines with 9 commas which is correct. | | What is (or may be) wrong with the first workbook? | | Thanks for your help. | -- | Jac Tremblay |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
CSV File format problem
Hi Jim,
This macro does work well and I thank you for your help. But I still want to understand why one should have to use such a macro when Excel is supposed to do that job automatically. Is it possible that some cell have a text format while the others below do not? Because, when I copy that last cell down to the last row, the problem does not appear and Excel treats these cells as if they had something in them. I guess I will have to live with it... Thank you again. "Jim Rech" wrote: Your problem seems similar to the one discussed he http://support.microsoft.com/default...95&Product=xlw You might have to use a macro to get the result you want. Try this one: 'Puts no quotes around strings ''Outputs the selection if more than one cell is selected, else entire sheet ''Include commas for missing items at end of row. Sub OutputActiveSheetAsTrueCSVFileAllCommas() Dim SrcRg As Range Dim CurrRow As Range Dim CurrCell As Range Dim CurrTextStr As String Dim ListSep As String Dim FName As Variant Dim ColCount As Integer Dim CurrCol As Integer FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv") If FName < False Then ListSep = Application.International(xlListSeparator) If Selection.Cells.Count 1 Then Set SrcRg = Selection Else Set SrcRg = ActiveSheet.UsedRange End If ColCount = SrcRg.Columns.Count Open FName For Output As #1 For Each CurrRow In SrcRg.Rows CurrCol = 0 CurrTextStr = "" For Each CurrCell In CurrRow.Cells CurrCol = CurrCol + 1 CurrTextStr = CurrTextStr & CurrCell.Value & IIf(CurrCol < ColCount, ListSep, "") Next Print #1, CurrTextStr Next Close #1 End If End Sub -- Jim Rech Excel MVP "Jac Tremblay" wrote in message ... | Hi everyone, | | I get a problem with a worksheet when I export the data in a csv file. I have a worksheet with 10 rows and 10 columns of data (100 cells). Not all of them are filled. The last cell is J10. | If I export this worksheet to a csv file, I get 9 commas separating the data on each of the 10 lines. That's fine. | If I add data in the range D11:D15, and reexport the worksheet to a csv file, I get 10 lines with 9 commas and 5 lines with 3 commas. That's not what I want. And that's not normal. | | If I use a new worksheet to test my problem, I get 15 lines with 9 commas which is correct. | | What is (or may be) wrong with the first workbook? | | Thanks for your help. | -- | Jac Tremblay |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
date format problem from opening dbf file in excel 2007 | Excel Discussion (Misc queries) | |||
Link from Chart File to Data File / Format Problem | Charts and Charting in Excel | |||
Excel file is no longer "in a recofnizable format" problem? | Excel Discussion (Misc queries) | |||
Date format/Writing to a file problem | Excel Discussion (Misc queries) | |||
Problem when multipple users access shared xl-file at the same time, macrocode for solve this problem? | Excel Programming |