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
Ken
Try this. When you make your selections, only pick the cells from the first column (A2,A3,A6,A7...)
ryloSub 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
Brilliant! Output is as described. Might need some slight adjustment after working with full data set, but so far so good!
Thanks!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks