Create individual Text file from each Excel 2003 row using Macro

jayant_1970

New Member
Joined
Dec 21, 2015
Messages
2
Hello !!

I am completely new to VBA. Can someone from community help me achieve following.

I have an excel 2003 worksheet with three columns data as follows:


SYMBOLLISTING_DATEFV
20MICRONS06-Oct-085
3IINFOTECH22-Apr-0510
3MINDIA13-Aug-041
8KMILES29-Jan-1410
A2ZINFRA23-Dec-102

<tbody>
</tbody>











1st Row is with heading text (SYMBOL , LISTING_DATE , FV) in column A, B & C.
Column A contains list of SYMBOLS from A2:A1700.
Column B contains list of LISTING_DATE from B2:B1700
Column C contains list of FV from C2:C1700

- I want to create individual .txt files with A2 to A1700 as file name. (example: 20MICRONS.txt)
- Each text file to contains corresponding data from Column B and Column C with comma and space in between them.
example: 20MICRONS.txt file contains data "LISTING_DATE = 06-Oct-08 , FV = 5" in single line.

Any VBA expert help me please.

Thanks in advance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi,

Assuming that the sheet containing the data is the active sheet, try something like this...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] CreateTextFiles()

    [COLOR=darkblue]Dim[/COLOR] sDestPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] vData [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]If[/COLOR] ActiveSheet [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    
    sDestPath = "C:\Users\Domenic\Desktop\" [COLOR=green]'change the destination path accordingly[/COLOR]
    [COLOR=darkblue]If[/COLOR] Len(sDestPath) > 0 [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]If[/COLOR] Right(sDestPath, 1) <> "\" [COLOR=darkblue]Then[/COLOR]
            sDestPath = sDestPath & "\"
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "Path not found.", vbCritical
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    vData = Range("A1:C" & Cells(Rows.Count, "A").End(xlUp).Row).Value
    
    [COLOR=darkblue]For[/COLOR] i = 2 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](vData, 1)
        [COLOR=darkblue]If[/COLOR] Len(vData(i, 1)) > 0 [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]Open[/COLOR] sDestPath & vData(i, 1) & ".txt" [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Output[/COLOR] [COLOR=darkblue]As[/COLOR] #1
                [COLOR=darkblue]Print[/COLOR] #1, vData(1, 2) & " = " & Format(vData(i, 2), "dd-mmm-yy") & ", " & vData(1, 3) & " = " & vData(i, 3)
            [COLOR=darkblue]Close[/COLOR] #1
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] i
    
    MsgBox "Completed...", vbInformation
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0
Thank you very much for a code Domenic !!!

I run this code in my excel sheet with change in destination folder. Code works flawlessly.

Thanks for taking your valuable time out to write this code.

This provide me an impetus to learn VB now, as a new hobby in my spare time.
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,258
Members
449,307
Latest member
Andile

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