VBA to save rows as CSV without empty rows

Jorgi

Board Regular
Joined
Jul 7, 2021
Messages
52
Office Version
  1. 2019
Platform
  1. Windows
Hello may I ask you to help me with VBA. I would like to save data via macro as CSV but only if there is a data. Every 6 rows equal to one piece of data element but sometimes the 6 rows can be empty (apart from column A) because there is no data and the file will have to be saved without the 6 empty rows. Thank you very much for your help

Initial file
1634825593353.png

outcome from macro
1634825869136.png
 
So in red the mod to apply :​
Rich (BB code):
For Each Rw In ActiveSheet.UsedRange.Columns("B:AZ").Rows
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
So in red the mod to apply :​
Rich (BB code):
For Each Rw In ActiveSheet.UsedRange.Columns("B:AZ").Rows
Marc L - thank you so much again it works perfectly. You are a lifesaver.
 
Upvote 0
So in red the mod to apply :​
Rich (BB code):
For Each Rw In ActiveSheet.UsedRange.Columns("B:AZ").Rows
Marc L, the .Columns ("B:AZ") works perfect but when I was testing it I noticed that if there is a formula in any cell and return "" (empty cell) then the empty line is in the outcome CSV file but it shouldn't be there. Apologies I should mention that there will be a formula and some formulas will be retuning empty cell "" if there will be no data. Is there any solution for this problem? Thank you

initial file
1635099261898.png

outcome file
1635099343669.png
 
Upvote 0
New fix :​
Rich (BB code):
If (Rw.Cells.Count - CountBlank(Rw)) > 1 Then Print #F, Join(.Index(Rw.Value2, 1, 0), ",")
 
Last edited:
Upvote 0
New fix :​
Rich (BB code):
If (Rw.Cells.Count - CountBlank(Rw)) > 1 Then Print #F, Join(.Index(Rw.Value2, 1, 0), ",")
Thank you Marc L I have run the macro and received an error.
1635157657683.png
 
Upvote 0
My bad as when editing the post for a better way I forgot the dot before the worksheet function CountBlank …​
 
Upvote 0
My bad as when editing the post for a better way I forgot the dot before the worksheet function CountBlank …​
My VBA is so bad I should spot it and not bother you. Marc L thank you so much again.
 
Upvote 0
My VBA is so bad I should spot it and not bother you. Marc L thank you so much again.
Mark L - how to make the CSV file saved via Macro to include the correct length of number string?

The Example: in the xlsx the number looks like 12345678912345 but when the macro save the file as CSV the number looks like 1.23457E+13. Is there any solution to have the number 12345678912345 in the CSV instead of 1.23457E+13? Thank you
 
Upvote 0

Check first the csv file with Notepad - or any other text file editor - rather than with Excel …​
 
Upvote 0
Check first the csv file with Notepad - or any other text file editor - rather than with Excel …​
Yes, notepad shows correct length of the number. Thank you again
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,756
Members
448,990
Latest member
Buzzlightyear

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