Excel to text with variable delimiters

ajelcick

New Member
Joined
Sep 26, 2011
Messages
2
Hello everyone. After digging around through various codes and tutorials and attempting to recall the last formal instruction on VBA I had in the late 90's, I still cannot seem to figure out if a VBA macro in excel can do what I need.

First off, I'm using Excel 2007 with VBA 6.5

That being said, I have an excel spreadsheet with a selection of data I would like to output to a text file, the catch is that the delimiter of the data changes from line to line in order to fit the expectations of the second program I'm feeding the data into. Additionally, the input excel file has data organized in columns, but that data will need to be transposed into a single delimited row.

The best idea I have would be a line by line read and output with its own delimiter, but I'm unsure how to go at this from a line by line standpoint.

Rather than describing by exact input and output parameters and begging for code, If someone could give me an outline or point me in the right direction so I can try and learn this myself, I would greatly appreciate it.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
ajelcick,

Welcome to the MrExcel forum.

You will generally get much more help (and faster) in this forum if you can post your small samples (sensitive data scrubbed/removed/changed) (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:
 
Upvote 0
You could use UDF. The one below will cocatenate a range of strings that meet specified requirements.

Suppose you have Name, Address and Phone columns in A:C

put the formula =ConcatIf(A1:C1, "<>", A1:C1, ",") in E1 and drag down.
Then each cell in E has a comma delimited string of the data for that row.

To take each of those and create a semi-colon delimited string, put
=ConcatIf(E:E, "<>", E:E, ";") in a cell.

Some approach like that should work for you.

Code:
Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
                    Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
                    
    Rem the first three argumens of ConcatIf mirror those of SUMIF
    Rem the Delimiter and NoDuplicates arguments are optional (default "" and False)
    Dim i As Long, j As Long
    
    With compareRange.Parent
        Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
    End With
    
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
                                            stringsRange.Column - compareRange.Column)
    
    For i = 1 To compareRange.Rows.Count
        For j = 1 To compareRange.Columns.Count
            If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                    ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
                End If
            End If
        Next j
    Next i
    ConcatIf = Mid(ConcatIf, Len(Delimiter) + 1)
End Function
 
Upvote 0
Thanks for the quick replies. Here is the format of the Input XLS with some sample data (without unused rows/columns:
Excel Workbook
CDEFGHIJKLMN
3123456789101112
4-0.016330.012667-0.010330.0146670.0066670.0186670.017667-0.00533-0.000330.005667-0.00933-0.03433
5-0.053330.9866670.7926671.6526671.4346671.3986671.7026671.6456671.3936671.7426671.6506670.040667
6-0.009330.6146670.4106671.3666671.0686671.2436671.2786671.4076671.3046671.5536671.4726670.010667
7-0.021330.3996670.2586671.1196671.0046670.8056670.8836671.0916671.0006671.1876671.1706670.019667
8-0.059330.1836670.1436670.7566670.5726670.4626670.5526670.7006670.7236670.8746670.784667-0.00433
9-0.036330.0606670.0486670.3456670.3176670.2726670.3046670.4076670.4056670.5376670.432667-0.00133
10-0.054330.0246670.0086670.2026670.1616670.1216670.1486670.1696670.1726670.3046670.251667-0.01133
11-0.04333-0.00733-0.022330.1296670.0616670.0606670.0846670.1016670.1236670.1616670.1426670.008667
370 tet p12 092311.pda
Excel 2007


Here is the desired output format with cell names used and required spacers indicated with []:

[space]A1[,][space]B1[,][']C1[,][']D1[,][']E1
[']F1
[']G1
[space x6][A2][,][space x6]B2[,][space x6]C2[,][space x6]<- REPEAT TO K2
[space]C4[,][space]C5[,][space]C6<-- REPEAT TO C11
[space]D4[,][space]D5[,][space]D6<-- REPEAT TO D11
[space]E4[,][space]E5[,][space]E6<-- REPEAT TO D11
[space]F4[,][space]F5[,][space]F6<-- REPEAT TO D11
[space]G4[,][space]G5[,][space]G6<-- REPEAT TO D11
 
Upvote 0
Try
=" "&A1&", ""&B1&",""&B1&",""&D1&",""&E1 in AA1
="'"&F1 in AA2
="""&G1 in AA3
=Rept(" ",6) & ConcatIf(A2:K2,"<>",A2:K2,","&Rept(" ",6)) & ","&Rept(" ",6) in AA4
=" " & ConcatIf(C4:C11,"<>",A2:K2,", ") in AB5 and drag right
Put the array formula =TRANSPOSE(AB5:ZZ5) in AA4:AA(many)
Column AA should be your output column
 
Upvote 0

Forum statistics

Threads
1,224,538
Messages
6,179,412
Members
452,912
Latest member
alicemil

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