+ Reply to Thread
Results 1 to 2 of 2

Thread: Excel to Txt Export

  1. #1
    Registered User
    Join Date
    10-06-2011
    Location
    India
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    1

    Question Excel to Txt Export

    Hi All

    I am new to this forum and also to Excel VBA.
    I want to create a macro that can export all values in Column A to a separate txt file in desktop.

    For example, if Column A looks like:

    1
    2
    3
    4
    5

    after running the macro, all the data only from current sheet column A is exported to txt file (along with newline character) and put it in desktop.

    I have no clue how to create a txt file through macro... Can some one please help.

    Thank you,

  2. #2
    Valued Forum Contributor Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    2010
    Posts
    952

    Re: Excel to Txt Export

    Welcome to the forum!

    Sub Test_StrToTXTFile()
      StrToTXTFile CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator & "ken.txt", _
        Join(WorksheetFunction.Transpose(Range("A1", Range("A" & Rows.Count).End(xlUp))), vbCrLf)
    End Sub
    
    Sub StrToTXTFile(filePath As String, str As String)
      Dim hFile As Integer
      If Dir(FolderPart(filePath), vbDirectory) = "" Then
        MsgBox filePath, vbCritical, "Missing Folder"
        Exit Sub
      End If
      
      hFile = FreeFile
      Open filePath For Output As #hFile
      If str <> "" Then Print #hFile, str
      Close hFile
    End Sub
    
    Function FolderPart(sPath As String) As String
      FolderPart = Left(sPath, InStrRev(sPath, "\"))
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0