Help saving and importing CSV in VBA... with accuracy!

Oliver Dewar

Board Regular
Joined
Apr 17, 2011
Messages
201
Hi All.

For my businesses CMS - I need for several excel workbooks to communicate with each other.

I'm trying to save a sheet as a csv... then import it into another workbook.

The problem I'm encountering is that no matter whether I comma or tab delimit, when I try to import the sheet, excel puts some column headers as rows under other column headers, and totally mixes the row data up.

If it makes any difference, my csv sheet has columns A to HN and each row has a large number of blank cells. There's currently about 5,000 rows but that will increase hugely in actual use.

I would have thought that an excel saved csv imported back into excel would be seemless... but it can't even get the column headers right.

There must be something simple I'm missing here. Am I unaware of a 'CSV 101' tip like "always fill blank spaces with a placeholder" or something? (I actually tried that to no avail!).

All help will be welcomed.

I'm in windows 7 and excel 2007.

Thanks all!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I can't test in Excel 2007 on Windows 7, but I can't reproduce your problem in Excel 2003 on Windows Vista. If I save this as CSV it is reproduced faithfully if I open it in Excel:

Excel Workbook
ABCDE
1Col1Col2Col3Col4Col5
2123
3234
4345
5456
6567
Gaps


The CSV file looks like this in Notepad:

Col1,Col2,Col3,Col4,Col5
123,,,,
,234,,,
,,345,,
,,,456,
,,,,567
 
Upvote 0
hi
why cant you add "|" between each cell in a row and make it as one cell before saving it as .CSV and while importing Use "|" as delimiter and import hope it will solve you problem

try as below

[TABLE="class: cms_table_grid, width: 407"]
<tbody>[TR]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E
[/TD]
[/TR]
[TR]
[TD]R200[/TD]
[TD]F342[/TD]
[TD]F321[/TD]
[TD]F222[/TD]
[TD]R200|F342|F321|F222|[/TD]
[/TR]
</tbody>[/TABLE]


Note:- after concatenate done with "|" do copy past (in my case Column "E") and delete A to D

[TABLE="class: grid, width: 151"]
<tbody>[TR]
[TD="align: center"]E[/TD]
[/TR]
[TR]
[TD="align: center"]R200|F342|F321|F222|[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Thank you both for your thoughts, especially you Andrew for trying to reproduce the problem.

The plot thickens... or perhaps gets clearer...

It seems the 'CSV 101' tip that I didn't know about was "CSV's don't like data with carraige returns in it!"

Some of my headers had carraige returns... not a problem - took them out and the data is at least importing with the right columns.

Problem 2... a lot of my data is text strings with carraige returns. How do I go about saving then importing a csv table full of carraige returns?

I've read that surrounding everything in "" sorts it... is there an easy way to put these around every cell in a massive table?

Cheers guys.
 
Upvote 0
ooh, just found my own answer - simply format the table to custom format \"@\"

Going to try the CSV process now and see if that does the trick...
 
Upvote 0
That's half an answer - this format doesn't put "" around numbers - which throws off the csv import. So on I look...
 
Upvote 0
I've got no idea Andrew... they were all created in form textboxes with return enter response set to true. How can I work out what the ASCII number is?
 
Upvote 0
Andrew... I'm struggling to find a way to put a double quote around every cell value - number, text or even the odd period (.) with nothing else... without fail.

If you can give me a solution to this - put double quotes around everything - I can leave the carriage returns, make sure there are no double quotes in the text strings by replacing them with inverted commas and everything will be sweet... (I hope!)
 
Upvote 0
Actually, I may have solved it... does this look any good? I'm going to test now:

With Selection
.Value = Evaluate("=if(len(" & .Address & "),""""""""&" & .Address & "&"""""""","""")")
End With
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top