+ Reply to Thread
Results 1 to 15 of 15

Create txt file from excel sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Create txt file from excel sheet

    I am a beginner in excel macro, and hope to find an answer to my question in this great forum.
    I want to create APRIL.txt file from the data in my spreadsheet.

    The sample Sheet is attached here with.
    Attached Files Attached Files
    My English is very poor, so please be patient >_<"

    Thanks & Regards.
    hkbhansali

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Create txt file from excel sheet

    If I may ask, what is the complication about this? I mean, if it's me I would just copy that and paste into a notepad and save.
    多么想要告诉你 我好喜欢你

  3. #3
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Create txt file from excel sheet

    This question's been asked a few times recently - so I've written a quick post to cover it:

    http://excel.solutions/2014/04/using...-to-text-file/
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  4. #4
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Create txt file from excel sheet

    Here's another..
    Change file path to suit..
    Private Sub CommandButton1_Click()
      Dim Rng As Range
            With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\April.txt", True)
                Set Rng = Cells(1, 1).Resize(Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row, 1)
                .writeline Join(Application.Transpose(Rng), vbCrLf)
            End With
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Re: Create txt file from excel sheet

    yaa, it s work,

    Thanks a lot all above my welwisher,

  6. #6
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Re: Create txt file from excel sheet

    Hi apo,
    will u add code if any row in a column A is mark "X" can skip in above code?

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Create txt file from excel sheet

    You can easily adapt my code to do that - see highlights in red:
    Sub WriteTextFile()
    
    Dim rng As Range, lRow As Long
    Dim stOutput As String, stNextLine As String, stSeparator As String
    Dim stFilename As String, stEncoding As String
    Dim fso As Object
    
    '-------------------------------------------------------------------------------------
    'CHANGE THESE PARAMETERS TO SUIT
    Set rng = ActiveSheet.UsedRange 'this is the range which will be written to text file
    stFilename = "C:\Temp\TextOutput.txt" 'this is the text file path / name
    stSeparator = vbTab 'e.g. for comma seperated value, change this to ","
    stEncoding = "UTF-8" 'e.g. "UTF-8", "ASCII"
    '-------------------------------------------------------------------------------------
    
    For lRow = 1 To rng.Rows.Count
        If rng.Cells(lRow, 1).Value <> "X" Then
            If rng.Columns.Count = 1 Then
                stNextLine = rng.Rows(lRow).Value
            Else
                stNextLine = Join$(Application.Transpose(Application.Transpose(rng.Rows(lRow).Value)), stSeparator)
            End If
            If stOutput = "" Then
                stOutput = stNextLine
            Else
                stOutput = stOutput & vbCrLf & stNextLine
            End If
        End If
    Next lRow
    
    Set fso = CreateObject("ADODB.Stream")
    With fso
        .Type = 2
        .Charset = stEncoding
        .Open
        .WriteText stOutput
        .SaveToFile stFilename, 2
    End With
    Set fso = Nothing
    
    End Sub

  8. #8
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Re: Create txt file from excel sheet

    Hi olly,

    Thanks for prompt reply,
    its work fine,

  9. #9
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Re: Create txt file from excel sheet

    hi olly,
    your code is perfect but its generate all column while i want only column "B" data only

  10. #10
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Create txt file from excel sheet

    Quote Originally Posted by hkbhansali View Post
    hi olly,
    your code is perfect but its generate all column while i want only column "B" data only
    Come on, you need to start understanding and tweaking the code to suit your workbook.

    Change the line highlighted in blue, to the range you actually want this to apply to:
    Sub WriteTextFile()
    
    Dim rng As Range, lRow As Long
    Dim stOutput As String, stNextLine As String, stSeparator As String
    Dim stFilename As String, stEncoding As String
    Dim fso As Object
    
    '-------------------------------------------------------------------------------------
    'CHANGE THESE PARAMETERS TO SUIT
    Set rng = ActiveSheet.UsedRange 'this is the range which will be written to text file
    stFilename = "C:\Temp\TextOutput.txt" 'this is the text file path / name
    stSeparator = vbTab 'e.g. for comma seperated value, change this to ","
    stEncoding = "UTF-8" 'e.g. "UTF-8", "ASCII"
    '-------------------------------------------------------------------------------------
    
    For lRow = 1 To rng.Rows.Count
        If rng.Cells(lRow, 1).Value <> "X" Then
            If rng.Columns.Count = 1 Then
                stNextLine = rng.Rows(lRow).Value
            Else
                stNextLine = Join$(Application.Transpose(Application.Transpose(rng.Rows(lRow).Value)), stSeparator)
            End If
            If stOutput = "" Then
                stOutput = stNextLine
            Else
                stOutput = stOutput & vbCrLf & stNextLine
            End If
        End If
    Next lRow
    
    Set fso = CreateObject("ADODB.Stream")
    With fso
        .Type = 2
        .Charset = stEncoding
        .Open
        .WriteText stOutput
        .SaveToFile stFilename, 2
    End With
    Set fso = Nothing
    
    End Sub
    So in your case, you may change this to something like:
    Set rng = ActiveSheet.Range ("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)

  11. #11
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Re: Create txt file from excel sheet

    Hi,olly, & apo
    THANKS,
    my problem solved, thanks for kind help.

  12. #12
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Re: Create txt file from excel sheet

    Hi,olly, & apo
    THANKS,
    my problem solved, thanks for kind help.

  13. #13
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Create txt file from excel sheet

    Hi..
    Given that all your data is in 1 column.. this should do..

    Private Sub CommandButton1_Click()
        Dim Rng As Range
        With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\April.txt", True)
            For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
                Set Rng = Cells(i, 1)
                If Not Split(Cells(i, 1).Value, ",")(0) Like "*x*" Then .writeline Rng
            Next i
        End With
    End Sub

  14. #14
    Forum Contributor
    Join Date
    12-21-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    398

    Re: Create txt file from excel sheet

    Hi, apo
    u r right my data is in 1 column only and its "B" column,
    above code is not working.

  15. #15
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Create txt file from excel sheet

    Olly is right.. you should step through any code given to you in order to understand what it does.. and at least attempt to reverse engineer it..

    Minor changes that were needed in my code are shown in red..

    Private Sub CommandButton1_Click()
        Dim Rng As Range
        With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\April.txt", True)
            For i = 1 To Range("B" & Rows.Count).End(xlUp).Row
                Set Rng = Cells(i, 2)
                If Not Split(Cells(i, 2).Value, ",")(0) Like "*x*" Then .writeline Rng
            Next i
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] One Report, create multiple sheets, save each sheet in a seperate excel file
    By sammymalta in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-04-2013, 07:12 AM
  2. Create a *.txt file from an excel sheet
    By Woodbags in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-07-2013, 08:36 AM
  3. Filter, copy filtered data into another sheet, create a file of that sheet..
    By titushanke in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-17-2012, 02:26 PM
  4. Replies: 3
    Last Post: 09-30-2011, 04:35 AM
  5. [Macro excel] How to create and export a sheet into a *.inp (or *.txt) file
    By MrKermit in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2006, 03:30 AM

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