+ Reply to Thread
Results 1 to 17 of 17

Macro to access/edit/save a text file

Hybrid View

  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Chintu Raju,

    This macro will copy the data in column "A" of the Active Sheet to a text file. All variables that can be changed are marked in red. You should make all your changes to the code before saving it. You can change your file name, file directory, and starting row. You can call the macro CreateTextFile by attaching it to a command button or by using ALT+F8 to run it from the Macro Dialog.

    Place this Code in a Standard VBA Module
    Public RunWhen As Double
    
    Sub CreateTextFile()
    
      Dim fso As Object 
      Dim FileName As String
      Dim LastRow As Long
      Dim MyFile As Object
      Dim Overwrite As Boolean
      Dim R As Long
      Dim StartRow As Long
    
        Overwrite = True
        FileName = "c:\testfile.txt"    'Be sure to include the directory path
        StartRow = 1
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
          Set fso = CreateObject("Scripting.FileSystemObject")
          Set MyFile = fso.CreateTextFile(FileName, Overwrite)
            For R = StartRow To LastRow
              MyFile.WriteLine(Cells(R, "A").Text)
            Next R
          MyFile.Close
    
        Set fso = Nothing
        RunSaveToFile
    
    End Sub
    
    Sub RunSaveToFile()
    
      'Runs every hour - TimeSerial(hours, minutes, seconds)
        RunWhen = Now + TimeSerial(1,0,0)
        Application.OnTime EarliestTime:=RunWhen, Procedure:="CreateTextFile", Schedule:=True
    
    End Sub
    
    Sun StopSaveToFile()
    
        On Error Resume Next
        Application.OnTime EarliestTime:=RunWhen,Procedure:="CreateTextFile", Schedule:=False
    
    End Sub
    Place this code in the ThisWorkbook Module
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
        StopSaveToFile
    
    End Sub
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 04-21-2008 at 12:06 PM.

  2. #2
    Registered User
    Join Date
    04-16-2008
    Posts
    19
    Thanks for the quick response....

    I tried running this, but there seems to be a small problem... All the values in the Column A gets transferred into the txt file. After this... a empty value also gets saved onto the txt file. Is there anyway to avoid this ?

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Chintu Raju,

    If the last cell in column "A" contains one or spaces, the macro will consider this to be a non empty cell. Check the worksheet to see if there are any spaces in the line below the last one with visible data.

    Sincerely,
    Leith Ross

  4. #4
    Registered User
    Join Date
    04-16-2008
    Posts
    19
    I rechecked this but there seems to be no space character anywhere on the sheet. But still the extra empty line comes up on the text file.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1