VBA code to Export selection as .csv file (with specified seperator).

NessPJ

Active Member
Joined
May 10, 2011
Messages
418
Office Version
  1. 365
Hey Guys,

For a file i made i am looking for a piece of code which allows me to export a selection of cells to a .csv file directly.

I have a selection of cells, for example:
A10:AA5000 filled with VLOOKUP formulas.
I would like the selection to be made only for the cells that contain output (cells that do not contain output are shown as blank by the formula).
So for example only selection A10:AA3499 are filled with values (the rest below them do not show results from the formula).

I should also be able to specifiy the seperator used.

Right now i have a macro that copies the selection to a blank file and then saves it as a .csv this results in a csv file with the wrong seperator.
I would very much like to solve this by using some code rather then being dependant on the language settings of Windows/Office.

Thanks in advance for any help. :)
 
Who told you that? That will (and was) never be the case. You need to reread your excel books.

CSV files (and tab and any other separator used) are recognize empty values so it will be:
Code:
col1, col2, col3
1,a,b
2,,c
3,d


I am a bit confused to why you would only want the code to run on cells that contain output
because this would mean the result could be out of sync with other data in terms of columns.

For example, suppose your data looks like this:
col1col2col3
1ab
2
c
3
d

<tbody>
</tbody>

when you export this as CSV as you've mentioned,it would look like
Code:
col1, col2, col3
1, a, b
2, c
3, d

Then when you've imported the CSV file again for re-evaluation, it would look something like:
col1col2col3
1ab
2c
3d

<tbody>
</tbody>

which is totally out its original column!
Is this okay?
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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