Results 1 to 7 of 7

Thread: convert the data from .xlsx to .txt file: Export Excel cell values to delimeted text File

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Hi
    The macro seems to work OK using this version of the macro below and your uploaded files
    Here is the output Share ‘Alert..txt’ : https://app.box.com/s/vyco4q3kgo8isalq48f926ftroyptri2

    Code:
     Sub xlsxTotxt_LineSeperatorvbLf_valuesSeperatorComma()
    Rem 1 Workbooks info
    Dim Wb1 As Workbook ': Set Wb1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Alert..xls")
    Set Wb1 = Workbooks("Alert..xls")
    Dim Ws1 As Worksheet: Set Ws1 = Wb1.Worksheets.Item(1)
    Dim Lr As Long, Lc As Long
     Let Lr = Ws1.Cells.Range("A" & Ws1.Rows.Count & "").End(xlUp).Row
     Let Lc = Ws1.Cells.Item(1, Ws1.Columns.Count).End(xlToLeft).Column
    Dim arrIn() As Variant: Let arrIn() = Ws1.Range(Ws1.Range("A1"), Ws1.Cells.Item(Lr, Lc)).Value ' Data range in sample2.xlsx
    Rem 2 make text file long string
    Dim Rw As Long, Clm As Long '
        For Rw = 1 To Lr ' each row in Ws1
            For Clm = 1 To Lc ' each column for each row in Ws1
            Dim strTotalFile As String
             Let strTotalFile = strTotalFile & arrIn(Rw, Clm) & "," ' add a value and a seperator for this line
            Next Clm
         Let strTotalFile = Left(strTotalFile, Len(strTotalFile) - 1) ' this will take off the last  ,
         Let strTotalFile = strTotalFile & vbLf  ' this adds the line seperator wanted by Avinash  -  https://excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)/page30#post13348  -  You will see that vbLf is the separator for lines(records)
        Next Rw
     Let strTotalFile = Left(strTotalFile, Len(strTotalFile) - 1)     ' this takes off the last  vbLf
     Debug.Print strTotalFile
    Rem 3 make text file from the  total string
    Dim FileNum As Long
    Let FileNum = FreeFile(1)                                  ' https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/freefile-function
     Open ThisWorkbook.Path & "\Alert..txt" For Output As #FileNum
    ' Open ThisWorkbook.Path & "C:\Users\WolfieeeStyle\Desktop\Alert..txt" For Output As #FileNum  ' CHANGE TO SUIT  ' Will be made if not there
     Print #FileNum, strTotalFile '                      strTotalFile
     Close #FileNum
    
    End Sub
    Possibly your error was this
    Code:
     Open ThisWorkbook.Path & "C:\Users\WolfieeeStyle\Desktop\Alert..txt" For Output As #FileNum  ' CHANGE TO SUIT  ' Will be made if not there
     
    You probably meant to write this:
    Code:
     Open "C:\Users\WolfieeeStyle\Desktop\Alert..txt" For Output As #FileNum  ' CHANGE TO SUIT  ' Will be made if not there
     
    Or this
    Code:
     Open ThisWorkbook.Path & "\Alert..txt" For Output As #FileNum  ' CHANGE TO SUIT  ' Will be made if not there
     
    Alan

    P.S.
    Just to check that you understand what is ThisWorkbook.Path, run this
    Code:
    Sub WhatIsThisWorkbook_Path()
     MsgBox Prompt:=ThisWorkbook.Path
    End Sub
    Last edited by DocAElstein; 06-11-2020 at 10:52 AM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

Similar Threads

  1. Convert Text from .txt to columns in excel
    By ravichandavar in forum Excel Help
    Replies: 2
    Last Post: 07-21-2017, 07:35 AM
  2. Button to export data to a master file
    By aryanaveen in forum Excel Help
    Replies: 0
    Last Post: 01-17-2015, 02:35 AM
  3. Replies: 7
    Last Post: 05-20-2014, 02:10 AM
  4. populate default values in cell of a csv file
    By dhivya.enjoy in forum Excel Help
    Replies: 2
    Last Post: 10-23-2013, 12:59 PM
  5. Import text file to an Excel file
    By obed_cruz in forum Excel Help
    Replies: 5
    Last Post: 08-03-2011, 07:58 PM

Tags for this Thread

Posting Permissions

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