+ Reply to Thread
Results 1 to 6 of 6

Inserting column headers text before the content of each cell in a column

  1. #1
    Registered User
    Join Date
    04-23-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Inserting column headers text before the content of each cell in a column

    Hi,

    I'm a complete novice at Excel VBA so please forgive me if this is something easy.

    I have a spreadsheet in the format below:

    Column 1: Column 2: Column 3:
    Dave 1 a
    Mike 2 b
    Jon 3 c


    I have some code that I managed to adapt from a previous thread that extracts the content of each row and saves it to individual text files. The only problem is that without the column headers I can't tell what some of the data means. I need a way to insert/concatenate the column headers with the content of their columns before it starts extracting the rows into separate files eg A2 would say "Column 1: Dave"

    Below is the code I have to extract the data into text files.

    Sub Export_to_individual_files_with_carriage_return()
    Dim r As Range, r2 As Range
    Dim ff As Integer
    Dim s As String

    For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
    s = ""
    For Each r2 In Range(r, Cells(r.Row, Columns.Count).End(xlToLeft))
    If VarType(r2) = vbString Then
    s = s & Chr(10) & "" & r2.Value & ""
    Else
    s = s & Chr(10) & r2.Value
    End If
    Next r2
    s = Right(s, Len(s) - 1)
    ff = FreeFile
    Open ThisWorkbook.Path & "\" & r.Value & ".txt" For Output As #ff
    Print #ff, s
    Close #ff
    Next r
    End Sub
    Any help you can offer would be appreciated.

    Thanks

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Inserting column headers text before the content of each cell in a column

    s = "" change to s=range("a" & r.row).value

    i think
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    04-23-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Inserting column headers text before the content of each cell in a column

    Thanks for reply.

    That doesn't quite work. In the file it extracts it gave me a first cell that said "Dave: Dave" and doesn't do anything to subsequent cells.

    I changed r.row to r.column and that gives me "Column 1: Dave" which is what I want. However it only does this for the data in the first column.

    Thanks

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Inserting column headers text before the content of each cell in a column

    Can you send me the sheet, and i'll sort.

  5. #5
    Registered User
    Join Date
    04-23-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Inserting column headers text before the content of each cell in a column

    Thank you,

    Is there any particular way to send it to you?

    The data in the spreadsheet is all made up test data so you could just mock something up and create a macro using the code below:


    Sub Export_to_individual_files_with_carriage_return()
    Dim r As Range, r2 As Range
    Dim ff As Integer
    Dim s As String

    For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
    s = Range("a" & r.Column).Value
    For Each r2 In Range(r, Cells(r.Row, Columns.Count).End(xlToLeft))
    If VarType(r2) = vbString Then
    s = s & Chr(10) & "" & r2.Value & ""
    Else
    s = s & Chr(10) & r2.Value
    End If
    Next r2
    s = Right(s, Len(s) - 1)
    ff = FreeFile
    Open ThisWorkbook.Path & "\" & r.Value & ".txt" For Output As #ff
    Print #ff, s
    Close #ff
    Next r
    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Inserting column headers text before the content of each cell in a column

    Something like this, a bit simpler to

    Dim r As Range
    Dim i As Integer
    Dim s As String

    Please Login or Register  to view this content.

+ 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] Move Data From One Column to Another based on column headers and Row Text
    By DerbysGal in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 02-10-2014, 07:21 AM
  2. [SOLVED] Match value(s) in column with headers of another sheet and paste a cell value from column
    By Zoediak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-17-2013, 11:34 AM
  3. inserting text under the last used cell in a column
    By skavety in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-10-2013, 09:45 AM
  4. [SOLVED] Loop through column headers to search from column name and get cell range
    By Pie in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-28-2005, 08:20 PM
  5. Autofit Column Headers with wrapped text (Not a merged cell issue)
    By Mark in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-12-2005, 08:05 PM

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