+ Reply to Thread
Results 1 to 5 of 5

Autorun Sub WriteToTextFile ()

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2010
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    27

    Autorun Sub WriteToTextFile ()

    Hi,

    I can find a lot of information how to autorun macros and subs when opening Excel 2007.

    I have one VBA sub "WriteTxt ()" which writes information from excel to text file. It works only when pressing F5 in VBA window.

    Is it possible to get it running automatically by entering a value into a cell?

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Autorun Sub WriteToTextFile ()

    Hi,

    How about something like this, which needs to go in the workbook area of the VBA project.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Address = "$A$1" And Sh.Name = "Sheet1" And Target.Value = 123 Then Call WriteTxt
    End Sub
    Obviously, amend the cell address, sheet name and value to your requirements
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    11-22-2010
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    27

    Re: Autorun Sub WriteToTextFile ()

    Should it look like this?
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Address = "$A$1" And Sh.Name = "Sheet1" And Target.Value = 123 Then Call WriteTxt
    End Sub
    
    
    Sub WriteTxt()
        ActiveWorkbook.SaveAs "C:\OUCHPut.txt", 21
    End Sub


    I tried this too:
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Intersect(Target, Range("A1")) And Target.Value = 123 Then Call WriteTxt
    End Sub
    
    
    Sub WriteTxt()
        ActiveWorkbook.SaveAs "C:\OUCHPut.txt", 21
    End Sub
    Last edited by Leith Ross; 12-17-2010 at 12:29 PM. Reason: Added Code Tags

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Autorun Sub WriteToTextFile ()

    I think that your WriteTxt() macro should go in a standard module, not in the worksheet area.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Autorun Sub WriteToTextFile ()

    Put this code in the sheetmodule
    There's no need for a separate macro.

    Private Sub Worksheet_Change(ByVal Target As Range)
      Target.Address ="$A$1" and Target.Value="#" Then ThisWorkbook.SaveAs "C:\examlple.txt", 21
    End Sub
    See the attachment.
    NB. the macro will be executed on the moment you enter 'enter' after entering # in A1.
    Attached Files Attached Files
    Last edited by snb; 12-18-2010 at 11:28 AM.



+ 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