Consulting

Results 1 to 5 of 5

Thread: Help, i need to create a command button for saving selection as txt file

  1. #1
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location

    Help, i need to create a command button for saving selection as txt file

    Hi,
    i am newbie in excel scripting and i need help for creating a command button.
    I need the button to save the selected row in a specified text file.
    Then when i select another row, i want the button to save the newly selected row to the same text file by rewriting the existing one.
    If possible i need the button to save(rewrite) the file automatically without asking confirmation.

    I found the following code which works but it saves the entire workbook, and asks confirmation. I can't figure out how to replace "ActiveWorkbook." with active selection
    "ActiveWorkbook.SaveAs Filename:="C:\filepath\example.txt", _
    FileFormat:=xlUnicodeText, CreateBackup:=False"

    Any help will be very appreciated!!!

    Thank you!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Something like
    Sub Test()
    Dim r As Range
    Set r = Intersect(ActiveSheet.UsedRange, ActiveCell.EntireRow)
    txt = Join(Application.Transpose(Application.Transpose(r.Value)), "|")
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\TestDir\testfile.txt", True)
    a.WriteLine txt
    a.Close
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location
    Quote Originally Posted by mdmackillop View Post
    Something like
    Sub Test()
    Dim r As Range
    Set r = Intersect(ActiveSheet.UsedRange, ActiveCell.EntireRow)
    txt = Join(Application.Transpose(Application.Transpose(r.Value)), "|")
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\TestDir\testfile.txt", True)
    a.WriteLine txt
    a.Close
    End Sub
    Thank you very much for your help. I will try your code right way. Thank you!

  4. #4
    VBAX Regular
    Joined
    Jul 2016
    Posts
    8
    Location

    Thank you!!!!

    Quote Originally Posted by mdmackillop View Post
    Something like
    Sub Test()
    Dim r As Range
    Set r = Intersect(ActiveSheet.UsedRange, ActiveCell.EntireRow)
    txt = Join(Application.Transpose(Application.Transpose(r.Value)), "|")
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\TestDir\testfile.txt", True)
    a.WriteLine txt
    a.Close
    End Sub
    Amazing, it worked like a charm!!! Thank you very much!!!

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Please do not quote.

    An alternative:

    Sub M_snb()
        ActiveCell.EntireRow.Copy
        
        With GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
           .GetFromClipboard
           CreateObject("scripting.filesystemobject").createtextfile("G:\OF\new.txt").write .GetText
        End With
    End Sub

Posting Permissions

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