Results 1 to 17 of 17

Macro to access/edit/save a text file

Threaded View

Chintu Raju Macro to access/edit/save a... 04-21-2008, 11:06 AM
Leith Ross Hello Chintu Raju, This... 04-21-2008, 12:03 PM
Chintu Raju Thanks for the quick... 04-21-2008, 12:30 PM
Leith Ross Hello Chintu Raju, If the... 04-21-2008, 04:07 PM
Chintu Raju I rechecked this but there... 04-21-2008, 11:07 PM
jindon Paste the code onto the sheet... 04-21-2008, 11:53 PM
Chintu Raju I am sorry to say ... I didnt... 04-22-2008, 01:00 AM
jindon 1) right click on sheet tab -... 04-22-2008, 01:05 AM
Chintu Raju Unfortunately I will have to... 04-22-2008, 02:17 AM
akscooter Re: Macro to access/edit/save... 12-01-2010, 03:03 PM
  1. #2
    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.

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