Convert a table to text in excel - each cell on a new line [closed] - excel

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table in excel, in the example below each number represents a cell:
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
I would like to convert this table to a text file that looks like this:
11 12
13
14
15
21 22
23
24
25
31 32
33
34
35
I am aware of the transpose function and a table to text but I can't seem to achieve what I illustrated above.

Using a simple VBA is efficient to create the actual file.
This code dumps data from the range from A1 to the last used cell in column E of the activesheet, to a file C:\temp\dummy.txt
Please change your path to suit
Sub RipData()
Dim X
Dim lngRow As Long
Dim lngCol As Long
Dim objFSO As Object
Dim objTF As Object
X = Range([a1], Cells(Rows.Count, "E").End(xlUp))
Set objFSO = CreateObject("scripting.filesystemobject")
Set objTF = objFSO.createtextfile("C:\temp\dummy.txt")
For lngRow = 1 To UBound(X, 1)
objTF.writeline X(lngRow, 1) & vbTab & X(lngRow, 2)
For lngCol = 3 To UBound(X, 2)
objTF.writeline X(lngRow, lngCol)
Next
objTF.writeline
Next
objTF.Close
End Sub

I usually apply a combination of ROUND, MOD, and OFFSET to turn a tabular set of data into a single column. This problem has the added wrinkle of wanting to put the second number in a group in a column to the right while skipping that number in the first column. And you want to put place a spacer row between each group of numbers. Both of these requirements make for more complicated than usual formulas.
For the first column, beginning in cell A5, or in another cell in column A the row of which is an even multiple of 5, use the following formula,
= IFERROR(
OFFSET(
$A$1,
ROUNDDOWN( ROW(A5) / 5 - 1, 0),
MOD( ROW(A5), 5) + (MOD( ROW(A5), 5) <> 0)
) /
(MOD( ROW(A5), 5) <> 4),
""
)
and copying down the column. This assumes that the data rows begin in cell A1.
For the second column, beginning with the cell in column B to the right of the starting cell in column A, enter this formula,
= IFERROR(
OFFSET($A$1,
ROUNDDOWN( ROW(B5) / 5 - 1, 0),
MOD( ROW(B5), 5) + 1
) /
NOT( MOD( ROW(B5), 5) > 0),
""
)
again copying it down.
How the formulas work
Both the column A and the column B function are elaborations of OFFSET, which takes as its arguments a starting address, the number of rows down (or up) its result range will begin, and the number of columns to the right (or left) that the range will. (It also takes two more arguments, which are the width and the height of the range to return. Since we're only concerned with single cells, we can leave these two arguments out.)
For example, the row calculation for column A uses the expression
ROUNDDOWN(ROW(A5)/5-1,0).
In cell A5, this formula resolves to (5/5 - 1) or 0, with no rounding needed. So, the row offset is 0 from A1, which makes sense because the A5 formula is processing the first row of data.
In cell A6, the formula becomes 6/5 - 1, or 1.2 - 1, or 0.2, which rounds down to 0, again what we need since we're still grabbing numbers from row 1.
This continues until cell A10, when we get 10/5 - 1, or 1. We're finished with the first row of data (which had an offset of 0 rows from a1) and now move on to the second. The value of the row offset will continue as 1 up to cell A15, when it will go up by 1 again.
The calculation of the column offset is a bit trickier:
MOD(ROW(A5),5)+(MOD(ROW(A5),5)<>0))
The first term is MOD(ROW(A5),5). In cell A5, that becomes MOD(5, 5), with a result of 0 since the integer remainder of 5 / 5 is 0. Again, makes sense--the column offset of 0 from cell A1 means the we will pick up the value in column A. In cell A6, we've got MOD(6, 5), for a column offset of 1. This means the value in A6 will come from column B.
But we don't want that: the value in column B of each data row is supposed to be shown in column B of the result range. We need to skip from column A to column C to get the next value for column A of the results.
Hence the second term (MOD(ROW(A5),5)<>0)). This evaluates to TRUE for every row that is not an even multiple of 5--these are the rows that show a (result) value in column A and column B. TRUE evaluates to 1 when used in an arithmetic expression. So, we are adding 1 to the column offset when the formula is in rows 6, 7, 8, 9, 11, 12, etc., thereby skipping over column B of the data row.
Finally, the divisor (MOD(ROW(A5),5)<>4). This expression will evaluate to TRUE (or 1) when the row the formula is in does not have a remainder of 4 when divided by 5. That means it evaluates to FALSE or 0 only when the formula is in rows 9, 14, 19, etc.
These in fact are the rows for which we want a space between the groups of numbers. The purpose of this divisor is to produce a formula error when the formula is in those rows. We then catch that error in the IFERROR function that encloses the entire formula, and the output of the formula becomes "" -- the empty string.
I won't go through the column B expression, which uses the same kind of maneuvers to pick up the second value in each data row and show it beside the first values.
For convenience in copying and pasting to your worksheet, here are the unformatted versions of the formulas:
Cell A5 Formula: =IFERROR(OFFSET($A$1,ROUNDDOWN(ROW(A5)/5-1,0),MOD(ROW(A5),5)+(MOD(ROW(A5),5)<>0))/(MOD(ROW(A5),5)<>4),"")
Cell B5 Formula: =IFERROR(OFFSET($A$1,ROUNDDOWN(ROW(B5)/5-1,0),MOD(ROW(B5),5)+1)/NOT(MOD(ROW(B5),5)>0),"")

Related

Transform hourly data in minute timestep data

I have an Excel file with hourly data, that is 8760 values on one year.
I need to have data at the minute-step.
I need to take the hourly value, and copy it to the 60 cells over and over again, until I have 525 600 values, being my minute timestep.
I tried a couple things, but haven't been able to do anything.
Update/Solution :
Sub test()
Worksheets("MySheet").Activate
Dim i As Double 'minutes increment
Dim j As Integer 'hourly increment
Dim k As Double
k = 0 'initialization of k
Dim Var1 As Single
Dim Var2 As Single
For j = 1 To 8760
Var1 = Cells(j, 8).Value 'Row "j"/from 1 to 8760, column "8"/H --> hourly values
Var2 = Cells(j, 7).Value 'Row "j"/from 1 to 60, column "7"/G --> minutes values
For i = 1 To 60
k = k + 1 'incrementation of k to be able to insert the values in the 60 cells (corresponding to 60 minutes)
Cells(k + 3, 10) = Var1 'insert those values in the 10th column (J)
Cells(k + 3, 9) = Var2 'insert those values in the 10th column (I)
Next i
Next j
End Sub
This can also be done without VBA, as given in answer, with the function =INDEX.
If the data are stored in a column (let's assume in column A) and you want a result also stored in a colum (let's assume in column B), a formula similar to this one might work:
=INDEX(A:A,QUOTIENT(ROW(B1)-1,60)+1)
Place it in cell B1 and drag it down.
If the data are stored in a row (let's assume in row 1) and you want a result also stored in a row (let's assume in row 2), a formula similar to this one might work:
=INDEX(1:1,QUOTIENT(COLUMN(A2)-1,60)+1)
Place it in cell A2 and drag it right.
As you can see the formula are basically the same. They are different just because one will work by being dragged down, the other by beign dragged right. If your list do not start from A1, the formulas can be easily adapted.
You could also write down a group of 60 cells the first one being 1, the following 59 being equal to the first one (by formula); then the 61st being would have been equal to the first plus 1 (always by formula), the following 59 equat to the 61st (again by formula). You can easily copy the second group as far as necessary. You then use a simple index formula linked to your new sequence of numbers to target the desired cell in the desired list.
Then again, you can also use VBA. It might be an overkill, though.
A data sample and/or more details might be needed if this answer isn't enouth.

looping through columns to adjust formula

i have 3 rows of data and 17 columns, in row 2 depending on a selection (low, med, high, which is cell referenced as say 1, 2, 3) i want to have formula that if low is selected row 2 would equal corresponding column in row 1 and add 1.
effectively some code i was thinking of is:
For i = 2 to 17
Cells(2,i).Formula = "Cells(1,i) + 1"
I need the output to actually be a formula and not a value though

repeating a formula conditionally in excel 2013?

I am trying to calculate a formula and then dragging it to apply it for the whole column but the problem is that I want to compare first cell of the column A with the first cell of the column B and then second cell of the column A with the second cell of the column B and then compare third cell of the column a with the first cell of the column B... :
A B result
1 1 4 0
2 2 5 0
3 3 0
4 4 0
5 5 1
6 5 1
when i write the pattern like if =IF((A1<Sheet1!B1),0,1) then =IF((A2<Sheet1!B2),0,1) ,=IF((A1<Sheet1!B1),0,1),=IF((A2<Sheet1!B2),0,1)
Four times and then dragging the formula for the column it start comparing it with the correspond one =IF((A5<Sheet1!B5),0,1) How should i change it ?
edit :
in the example I want to compare cell(1,A) with cell(1,B) then cell (2,A) with cell (2,B) and then cell(3,A) with cell(1,B) then cell (4,A) with cell (2,B) and then cell(5,A) with cell(1,B) then cell (6,A) with cell (2,B).[repeating the pattern two times and then start over]
Try this in C1,
=--NOT(A1<OFFSET('Different Sheet'!$B$1, MOD(ROW(1:1)-1, 2), 0))
Fill down as necessary. You only seem to be looking for a 0 or a 1 so I've simplified your IF statement. (Note: maths with MOD adjusted for more universality)
For different multiples you should only have to change the divisor parameter of the MOD function. The ROW function used as ROW(1:1) will return 1, 2, 3, 4, 5, etc as you fill down. MOD returns the remainder of a division operation so MOD(ROW(1:1)-1, 3) filled down will return 0, 1, 2, 0, 1, 2, 0, etc.
If you used the COUNT function on the numbers in 'Different Sheet'!B:B, you should be able to achieve a dynamic divisor.
=--NOT(A1<OFFSET('Different Sheet'!$B$1, MOD(ROW(1:1)-1, COUNT('Different Sheet'!B:B)), 0))

Excel: finding a given number criteria meeting cells from right to left

I need to find the average of 8 last (rightmost, from right to left) numeric cell values in a row that meet simple criteria of >= 0, ie. zero or positive numbers from rows that contain a mix of zeroes, and negative and positive values. In other words, I need to find the 8th cell reference from all cells containing 0 or higher counting from the end of the row backwards.
Example row:
1.6425 0.6233 5.2899 4.4372 2.0356 3.9796 1.5306 3.8344 0 -1 -1 3.8294 -1 3.0957 0 3.7572 -1
Expected result:
2.50
(3.7572 + 0 + 3.0957 + 3.8294 + 0 + 3.8344 + 1.5306 + 3.9796) / 8
I solved this cleanly using AVERAGEIFS and adding only one supplementary row A2:Q2 to mark just the 8 cells required. This extra row counts backwards from the end of the row, iterating by one from the previous count (next cell to the right) only if the cell above in row 1 is not less than 0, with the formula =IF(A1<0,B2,B2+1) copied throughout row A2:Q2. This then acts as criteria2 for the main formula i.e. use the first 8 cells from the right with a value >=0.
Then it's a simple =AVERAGEIFS(A1:Q1, A1:Q1, ">=0", A2:Q2, "<9") in cell C4 to get the result. C5 is a simple manual test against the calculated result.
Here's the Excel file: https://dl.dropbox.com/u/4974539/averageifs_tweak.xlsx - and a screen shot:
I solved your problem but with plenty of auxiliary columns. In the following I assume you have your data in columns A to Q, starting in row 2
On the first row, put letters A to J in columns T to AC
On the first data row
2.1 Put the forumula =ROW() in column S
2.2 Put array formula (press Shift + Ctrl + Enter when entering it) =SUM(IF(INDIRECT(T$1 & $S2 & ":Q" & $S2,TRUE)>=0,1,0)) to column T and copy it right all the way to column AC
2.3 Enter array formula =INDEX($T$1:$AC$1,MATCH(8,T2:AC2,0)) in column AD
2.4 Finally, put array formula =SUM(IF(INDIRECT(AD2 & S2 & ":Q" & S2) >= 0, INDIRECT(AD2 & S2 & ":Q" & S2), 0))/8 to column AE, this is the result you want
You can copy the row with formulas down to every row with data
This is how it looks in my Excel:
What it does:
in column S is the current row -- we need it for the INDIRECT function, because ROW() does not work in INDIRECT in array functions.
in columns T - AC we count the number of columns in the right side of the current row of data that are positive or zero, starting in different columns -- their letters are on the first row. The rightmost column where we can start is J, otherwise there would not be
8 values.
in column AD we match number 8 and from the first row we get the column where we have to start the range for averaging
finally, in column AE we use INDIRECT to create the reference to the range we want, sum all the numbers that are >= 0 and divide by 8

formula for finding the first non-empty cell

I have a spreadsheet with 2 columns of data, column A and B, and column C where I'm looking for the formula.
row A B C
1 50
2
3
4
5 56 6
6
7
8 46 10
9
10
11 64 18
As you can see, a row either contains a value or not. In column C, I'm looking to calculate the difference between
a) the value in column B and the value in column A's first non-empty cell directly above (so for example, at row 5, I calculate the difference between B5 and A1 56 - 50 = 6) if the value of B is a number and
b) the value in column A and the value in column B's first non-empty cell directly above (row 8, 56 - 46 = 10)
and put nothing if neither column A and B are empty.
I've been struggling with "first non-empty cell" to write an R1C1 formula. Note that I know I can do this in VBA but I'm looking for the R1C1 formula.
Thanks for your help.
Here is an array formula (that you need to validate with Ctrl + Shift + Enter) you can put in C1 and drag and drop till the end of your data:
=IF(OR(A1<>"",B1<>""),INDEX($B$1:B1,MAX(IF($B$1:B1="",0,ROW($B$1:B1))))-INDEX($A$1:A1,MAX(IF($A$1:A1="",0,ROW($A$1:A1)))),"")
Or, in a french version of Excel:
=SI(OU(A1<>"";B1<>"");INDEX($B$1:B1;MAX(SI($B$1:B1="";0;LIGNE($B$1:B1))))-INDEX($A$1:A1;MAX(SI($A$1:A1="";0;LIGNE($A$1:A1))));"")
Note that if you feel interested, you can commit into Stackoverflow in french
Perhaps try this formula in C2 copied down
=IF(B2="",IF(A2="","",LOOKUP(9.99E+307,B$1:B1)-A2),B2-LOOKUP(9.99E+307,A$1:A1))

Resources