Help write to a text file, a bit confused.

mrmagoo_83

Board Regular
Joined
Mar 7, 2005
Messages
82
I need some help. I have tried to do this on my own using other examples, but I am a bit lost.

I have a sheet in my excel file, a lot of data in column 1. I am wanting to open a text file and write all that data to that text file.

Each line in the column is like "The letter is", the macro adds a charater, so the column then looks like "The letter is A", then B, then C, etc...

I want to write the entire column to a text file such as A.txt (when everything is A) and B.txt when everything is (B).

I have the macro to run through and edit each line and all that; however, I cannot get this thing to open a file for nothing.

I tried the following, where the Filename was a variable that ends up looking like "C:\A.txt"; but i get an error 438 on the line that attempts to open the text file. Line is also a string Variable that has already been set to a value.

Code:
Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(Filename)
    Set ts = f.OpenAsTextFile(ForAppending, TristateTrue)
    ts.Write Line
    ts.Close

Can anyone help?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Well sort of, I am more wanting to create a new file with each incerment of the count.

Each files name will change by one, and the contents will change by one.

So I am not really appending the files, but creating new ones.

A.txt
B.txt
C.txt
etc....
 
Upvote 0
Here is what I am doing, exporting a specified range to a delimited file... I don't know if this helps, but I don't know if you can nail down a specific range for each text file.

Anyway... I hope this helps

Code:
Sub X() 
    SaveAsDelimited Range("'MAS90 Data'!A1:B4"), "," 
 End Sub 
  
Sub SaveAsDelimited(Data As Range, Delimiter As String) 
    Dim Fname As Variant 
    Dim strBuf As String 
    Dim rngTemp As Range 
    Dim rngCell As Range 
    Dim intUnit As Integer 
    Dim strDelimit As String 
    Dim strPath As String 
    Dim strName As String 
    
    MsgBox "Please enter the job number as the filename." 
    Fname = Application.GetSaveAsFilename( _ 
    fileFilter:="CSV (*.csv), *.csv") 
    If Fname = False Then 
    MsgBox "You didn't enter a filename!" 
    End If 
    
    intUnit = FreeFile 
    Open Fname For Output As intUnit 
    For Each rngTemp In Data.Rows 
        strBuf = "" 
        strDelimit = "" 
        For Each rngCell In rngTemp.Cells 
            If InStr(rngCell.Text, ",") > 0 Then 
                               strBuf = strBuf & strDelimit & """" & rngCell.Text & """" 
            Else 
                strBuf = strBuf & strDelimit & rngCell.Text 
            End If 
            strDelimit = Delimiter 
        Next 
        Print #intUnit, strBuf 
    Next 
    Close intUnit 
      
End Sub
 
Upvote 0
I will give that a shot, I am still not understanding all this FreeFile stuff, but I am seeing it in a lot of the code to do such things, so I will give it a try when i get to the casa.
 
Upvote 0
Open SaveFile For Output As #1 ' Open file and make it ready for output.
Print #1, strData
Close #1

This will create or recreate whatever file name you have in the savefile varaible. THen it puts the data from the strData in it.
 
Upvote 0
ok that works partially.

The problem is I have like a dozen lines that need to be written to each file, this only writes one, then overwrites it with the next.

If I misrespresented that somewhere I apologize.
 
Upvote 0
You would have to copy this sub for each range and then modify the range in the sub...

If you have dynamic data, that is to say

You may have A,B,C txt files today

and A,B,C,D text files tommorow, this will not work. Or it might I just don't know how to change it so that it will.
 
Upvote 0
Well, now it works, but it creates the files A 1.txt, instead of A1.txt

I do this:

FileNum = Str(Count + 1)

Which = " 1"

How the heck do I get rid of that space?
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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