+ Reply to Thread
Results 1 to 17 of 17

Macro to access/edit/save a text file

Hybrid View

  1. #1
    Registered User
    Join Date
    04-16-2008
    Posts
    19

    Macro to access/edit/save a text file

    I have an xl doc in which one of the sheet's column A changes value every 1 hour...

    What I would like to know is.. if there is a method in which i can copy these values from column A to a text file every hour...

    The range of cells containing values in Column A also varies every hour.

    Also, the old values in the text file needs to get deleted before the new values are updated every hour.

    Thanks in advance

  2. #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.

  3. #3
    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 ?

  4. #4
    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

  5. #5
    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.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    Paste the code onto the sheet module
    Private Sub Worksheet_Calculate()
    Dim fn As String
    fn = ThisWorkbook.Path & "\" & Me.Name & Format$(Time,"hh-mm-ss") & ".text"
    With Range("a1", Range("a" & Rows.Count).End(xlUp))
        Open fn For Output As #1
            Print #1, Join(Evaluate("transpose(" & .Address & ")"), vbCrLf)
        Close #1
    End With
    End Sub

  7. #7
    Registered User
    Join Date
    04-16-2008
    Posts
    19
    I am sorry to say ... I didnt get you completely.. Copy this code and then ?

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    Quote Originally Posted by Chintu Raju
    I am sorry to say ... I didnt get you completely.. Copy this code and then ?
    1) right click on sheet tab - [ViewCode]
    2) paste the code onto the right pane
    3) click x to close the window

    It will create a text file according to the change made through file import...
    So it doesn't need to have OnTime method.

    Aren't you updating Col.A through importing data from other file ? (query)

  9. #9
    Registered User
    Join Date
    04-16-2008
    Posts
    19
    Unfortunately I will have to use the Ontime method coz there are few calculations which happens on this Sheet (takes about 2 minutes for the final result), and then the final values update in Column A... But as you said if any value change in the Column A leads to making the text file, it would probably create it all the time when the calculation happens.

    Does it ?

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    If you need OnTime method anyway then
    Private myTime As Double
    
    Sub test()
    Dim fn As String
    fn = ThisWorkbook.Path & "\" & Me.Name & Format$(Time,"hh-mm-ss") & ".text"
    With Range("a1", Range("a" & Rows.Count).End(xlUp))
        Open fn For Output As #1
            Print #1, Join(Evaluate("transpose(" & .Address & ")"), vbCrLf)
        Close #1
    End With
    myTime = Now + TimeValue("01:00:00")
    Application.OnTime myTime, "test"
    End Sub
    
    Sub Finish()
    Application.OnTime myTime, "test", ,False
    End sub

  11. #11
    Registered User
    Join Date
    11-25-2010
    Location
    North Pole, Alaska
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to access/edit/save a text file

    I know this appears to be an old thread, I'd like to revive it because when I tried this, I get an error.

    I can set a key combo to activate the CreateTextFile sub, and the file is created/overwritten.

    I changed the line TimeSerial(1,0,0) to (0,1,0) so it runs every minute. Which it does.

    But when I run the sub RunSaveToFile, I get an error every minute.

    "Cannot run the macro "pathto.xlsm'!CreateTextFile'. The macro may not be available in the workbook or all macros may be disabled."

    I looked this error up online, and found how to enable all macros, as well as a macro patch for excel. Neither have resolved the problem. So now I ask you!

    When run manually, the CreateTextFile sub executes as advertised. It's when the timer executes the sub that the error occurs.

    Thanks in advance,
    Jon

+ 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