+ Reply to Thread
Results 1 to 4 of 4

VBA Code for Exporting Selected Columns in Excel to Notepad

Hybrid View

  1. #1
    Registered User
    Join Date
    02-08-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    10

    VBA Code for Exporting Selected Columns in Excel to Notepad

    Upon clicking a button, columns A -D will be copied to notepad. In the notepad, there should be additional items added on top of the values copied from Excel.

    Sample template of both excel and notepad as attached. Would greatly appreciate if anyone could set up the VBA codes or at least guide me on how I should start. Thanks.

    Sample1.xlsxSample1.txt

  2. #2
    Forum Contributor
    Join Date
    12-14-2013
    Location
    Tilburg, Nederland
    MS-Off Ver
    Excel 2010
    Posts
    256

    Re: VBA Code for Exporting Selected Columns in Excel to Notepad

    Sub Data2Text()
        Dim fs, obj As Object
        Dim Folder, FileName, Text As String
        Dim lR, i, c As Long
        
        Folder = ThisWorkbook.Path & "\"
        FileName = " Report_Sample.txt"
        lR = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
        For i = 2 To lR
            For c = 1 To 6
                Text = Text & Sheets(1).Cells(i, c).Value
                If c < 6 Then
                    Text = Text & "; ;"
                Else
                    Text = Text & vbNewLine
                End If
            Next
        Next
           Set fs = CreateObject("Scripting.FileSystemObject")
                Set obj = fs.CreateTextFile(Folder & FileName, True)
                    obj.WriteLine (Text)
                obj.Close
            Set fs = Nothing
    End Sub

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: VBA Code for Exporting Selected Columns in Excel to Notepad

    xace

    The values in the text file don't appear to match those in the Excel worksheet.

    This code will write the data to a text file using ;; as a delimiter.
    Dim strLine As String
    Dim rng As Range
    
        Set rng = Worksheets("Sheet1").Range("A2")
        
        Open "C:\test\MySample1.txt" For Output As #1
    
        Do Until rng.Value = ""
        
            strLine = Join(Application.Transpose(Application.Transpose(rng.Resize(, 6).Value)), ";;")
            
            Print #1, strLine
            
            Set rng = rng.Offset(1)
            
        Loop
        
        Close #1
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    02-08-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA Code for Exporting Selected Columns in Excel to Notepad

    thanks everyone for the quick response, i'll try it out and see if everything is fine

+ 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. how to copy selected notepad text and paste it in other notepad with macro
    By Guru232 in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 04-24-2013, 04:43 AM
  2. Exporting from excel 2010 to notepad or into a .hol file
    By ammacdo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-02-2012, 09:31 PM
  3. Exporting spreadsheet to notepad without space
    By amb1s1 in forum Excel General
    Replies: 4
    Last Post: 09-10-2011, 10:52 AM
  4. Automatic exporting in notepad text form from Excel Table
    By Horia in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2010, 09:39 AM
  5. Exporting Excel Data into Notepad
    By devender_g in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-01-2005, 06:55 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