Outputting a sheet to .csv with special formatting... (vba)

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a sheet which I need to output to .csv (or more specifically .txt)

I've posted the workbook to my webserver: http://www.bluecustard.co.uk/Book1.xlsm

Sheet 1 (DATA_FULL) holds the data which I need to export. I can do this using:

Code:
Sub ExportRange()
Dim FirstCol As Integer
Dim LastCol As Integer
Dim C As Integer
Dim FirstRow
Dim LastRow
Dim R
Dim data
Dim ExpRng As Range
Sheets("DATA_FULL").Range("A1").Select
    Set ExpRng = ActiveCell.CurrentRegion
    FirstCol = ExpRng.Columns(1).Column
    LastCol = FirstCol + ExpRng.Columns.Count - 1
    FirstRow = ExpRng.Rows(1).Row
    LastRow = FirstRow + ExpRng.Rows.Count - 1


    'Open ThisWorkbook.Path & "\textfile.csv" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  ' csv file
    Open ThisWorkbook.Path & "\textfile.txt" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]  '''''or txt file
        For R = FirstRow To LastRow
            For C = FirstCol To LastCol
                data = ExpRng.Cells(R, C).Value
                If data = "" Then data = ""
                If IsNumeric(data) Then data = Val(data)
                If C <> LastCol Then
                    Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , data;
                Else
                    Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , data
                End If
            Next C
        Next R
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
End Sub

The sheet is broken up into 18 sections and each section has 4 lines of 'headings' and then the subsequent data. Also, each 'block/section/ is separated by a blank row..

The code above outputs the entire sheet and replaces any 'space' with "",

However, my problem is that I need to format the output so that a blank row just outputs a blank row in the .txt file and not:
"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""

Secondly, it is outputting each section to the max number of columns, and I need to tailor each section to the number of columns per section. ie:

"GROUP","PROJ","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""
"HEADING","PROJ_ID","PROJ_NAME","PROJ_LOC","PROJ_CLNT","PROJ_CONT","PROJ_ENG","PROJ_MEMO","FILE_FSET","","","","","","","","","","","","","","","","","","","","","","","","","","",""
"UNIT","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""
"TYPE","ID","X","X","X","X","X","X","X","","","","","","","","","","","","","","","","","","","","","","","","","","",""
"DATA","123456","Newmarket","Newmarket","ACME Engineering","ACME Engineering","ACME Engineering","AGS DATA","","","","","","","","","","","","","","","","","","","","","","","","","","","",""
"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","" <- blank line

needs to be outputted as:

"GROUP","PROJ" '<- note this line is only 2 columns with no "","" - the rest span the max column
"HEADING","PROJ_ID","PROJ_NAME","PROJ_LOC","PROJ_CLNT","PROJ_CONT","PROJ_ENG","PROJ_MEMO","FILE_FSET"
"UNIT","","","","","","","",""
"TYPE","ID","X","X","X","X","X","X","X"
"DATA","123456","Newmarket","Newmarket","ACME Engineering","ACME Engineering","ACME Engineering","AGS DATA",""
this line is a space with no "",""

In my workbook, I have each section broken down into separate sheets. Perhaps the .txt file could be built up bit by bit so that it conforms to the above format?

I've searched everywhere for a possible solution, but have drawn a blank.

If you can help me out with my problem, or push me in the right direction, I'd be very grateful

Thanks very much!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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