Closed Thread
Results 1 to 6 of 6

macro to copy cells to text files

Hybrid View

docaia macro to copy cells to text... 02-04-2008, 10:46 AM
Paul Hi docaia, What are your... 02-05-2008, 12:07 AM
docaia format of output file 02-06-2008, 04:24 AM
docaia Macro to paste data to... 02-07-2008, 07:04 AM
docaia send to text till last cell 02-27-2008, 03:26 PM
docaia Bump 02-28-2008, 03:30 AM
  1. #1
    Registered User
    Join Date
    02-04-2008
    Posts
    11

    macro to copy cells to text files

    I am a beginner in Macro writing, I can only record a macro or do some basic VB commands, so I have a very basic question.
    I need a macro to copy a cell or a range of cells to text file, i.e when the macro runs, it copies your specified cells with their correct order to a normal text file

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887
    Hi docaia,

    What are your 'specified cells' and what order would you want them written to the file?

    An example of writing cells to a file is shown below:
    Sub makeFile()
        Dim ce As Range
        Open "c:\testing.txt" For Output As #1
        For Each ce In Range("A1:A10")
            Write #1, ce.Value
        Next ce
        Close #1
    End Sub
    Last edited by Paul; 02-05-2008 at 12:19 AM.

  3. #3
    Registered User
    Join Date
    02-04-2008
    Posts
    11

    format of output file

    thanks for your reply,
    suppose I want to copy the range you specified but at column number 13 of the output text file in the example, how can this be done?

  4. #4
    Registered User
    Join Date
    02-04-2008
    Posts
    11

    Macro to paste data to specific text column

    I used your code and it works fine,
    Sub makeFile()
    
        Dim ce As Range
    
        Open "c:\codes.txt" For Output As #1
    
        For Each ce In Range("A1:A20")
    
            Write #1, ce.Value
    
        Next ce
    
        Close #1
    
    End Sub
    the question now is how can I paste these cells to a specified column in the text file, like copying the cells in the range A1 to A20 to column # 1 in the text file, and copying the values in the range C1 to C20 to column # 13 for example

    any help with that will be much appreciated, it will save me a lot of time

    thanks
    Last edited by VBA Noob; 02-27-2008 at 03:31 PM.

  5. #5
    Registered User
    Join Date
    02-04-2008
    Posts
    11

    send to text till last cell

    I tried the following code to copy cells to text file so that the macro searches for the cells till the last cell that has data and exports them to text file, this is the code I used:
    Sub prn()
    last = Cells(Rows.Count, 1).End(xlUp)
    Dim txt As String
    txt = "d:/textfile.txt"
    Open "d:/textfile.txt" For Output As #1
    Dim i As Integer
    For i = 1 To last
    A = Cells(i, 1) & "    " & Cells(i, 2)
    Print #1, A
    Next i
    Close #1
    Dim x
        x = Shell("notepad.exe d:\textfile.txt", 1)
    '
    End Sub
    the code works fine till a specific number of rows, I think till 10 rows, if more rows are introduced it begins exporting only the first 3 or 4 rows and ignores the rest, in a reverse way, i.e if I intoduce 11 rows, 9 rows are exported to text, if 12 rows are written then 8 rows are exported to text and so on.
    can some one tell me please what is wrong with this code?
    Last edited by docaia; 02-28-2008 at 03:25 AM. Reason: added more details

  6. #6
    Registered User
    Join Date
    02-04-2008
    Posts
    11

    Bump

    Bump this post please

Closed Thread

Thread Information

Users Browsing this Thread

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

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