+ Reply to Thread
Results 1 to 3 of 3

Thread: Output of Highlighted Rows to Text File

  1. #1
    Registered User
    Join Date
    02-05-2008
    Posts
    5

    Output of Highlighted Rows to Text File

    I'm entering several rows of information into a worksheet, with several columns per row. Row 1 contains the column headers and rows 2 through n contain values. I want to select multiple rows - typically skipping rows, but sometimes several in a group - and then output the highlighted information to a text file via a macro. The format must be modified though to a single column and include the column headers as labels, with all values for the first row exported first followed by each of the other highlighted rows.

    As a simple example, using values in these cells, with similar information in several more rows:

    A1 (header): Name
    B1 (header): State
    C1 (header): Records
    A2 (value): John
    B2 (value): VA
    C2 (value): 14
    A3 (value): Bob
    B3 (value): WA
    C3 (value): 17
    Etc...

    I want to select rows 2, 3, 6, 7, etc., and output to a text file in this format:

    Name=John
    State=VA
    Records=14
    Name=Bob
    State=WA
    Records=17
    Name=Steve
    State=CA
    Records=22
    (Etc)

    The correct column header is displayed, followed by =, and then the value, outputting each row completely before proceeding to the next row. Empty cells should also be included in the output, but with only the header, e.g. Records=.

    Thanks,
    Ken

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359
    Ken

    Try this. When you make your selections, only pick the cells from the first column (A2,A3,A6,A7...)

    Sub bbb()
      Dim arr As Variant
      ReDim arr(0)
      For Each ce In Selection
        arr(UBound(arr)) = ce.Row
        ReDim Preserve arr(UBound(arr) + 1)
      Next ce
      ReDim Preserve arr(UBound(arr) - 1)
      
      Open "c:\temp\aa.txt" For Output As #1
      For I = LBound(arr) To UBound(arr)
        For j = 1 To 3
          Print #1, Cells(1, j).Value & "=" & Cells(arr(I), j).Value
        Next j
      Next I
      
      
      Close #1
    End Sub
    rylo

  3. #3
    Registered User
    Join Date
    02-05-2008
    Posts
    5
    Brilliant! Output is as described. Might need some slight adjustment after working with full data set, but so far so good!

    Thanks!

+ Reply to 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.2.0