Transform hourly data in minute timestep data - excel

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.

Related

Facing issues when joining rows of A column data into single cell at B column of 1st row

I just want to bifurcate around 14900 rows of A column into 50 sets at each row in B column keeping # as delimiter. Example: 12345#5432333#3232#... till 50th row of A column. Like that I need to build 50 sets till row count of A column(14900) in each row of B column.
I developed code for the same using =TEXTJOIN("#",TRUE,A1:A14900) but this is failing, may be due to length issues in single cell. my plan is to break the output of this formula into 50 sets and place the same into each row of B column. if i use loop to make the formula dynamic then im worrying the length of cell again
-------- --------
12345566 12345566#333344444#98ZBRE322#1ZAZAZAQQ...till 50th row
333344444 next 50 sets
98ZBRE322 next 50 sets
1ZAZAZAQQ next 50 sets
Is there any best way to solve this issue?
Concatenate Ranges
Excel
In cell B1 use:
INDEX
=TEXTJOIN("#",TRUE,INDEX(A$1:A$14900,(ROW()-1)*298+1):INDEX(A$1:A$14900,ROW()*298))
OFFSET (Volatile)
=TEXTJOIN("#",TRUE,OFFSET(A$1:A$298,(ROW()-1)*298,0))
and copy down to cell B50.
VBA
Sub ConcatSets()
With ActiveSheet.Range("B1:B50")
' INDEX
.Formula = "=TEXTJOIN(""#"",TRUE,INDEX(A$1:A$14900,(ROW()-1)*298+1):INDEX(A$1:A$14900,ROW()*298))"
' OFFSET (Volatile)
'.Formula = "=TEXTJOIN(""#"",TRUE,OFFSET($A$1:$A$298,(ROW()-1)*298,0))"
' To keep only values use the following:
'.Value = .Value
End With
End Sub

Sum values in the column, if another column data are equal

I'm trying to analyze the data with >10K rows.
It contains 2 columns. 1st column has ID number. This number is repeating from row to row different times.
Columns 2 has just numbers, which I want to sum up if value in column 1 is the same.
For example: Image showing example attached.
First, what I did - is filtered from A/Z and trying with cycle find out qty of repeating items:
Private Sub CommandButton1_Click()
Dim row, B, i, col As Long
Dim H As Worksheet
Set H = Sheets("Sheet1")
H.Activate
row = H.Cells(Rows.Count, 1).End(xlUp).row
For B = 4 To row
i = 1
j = H.Cells(B, 2).Value
If H.Cells(B, 1).Value = H.Cells(B + 1, 1).Value Then
i = i + 1
.....
Result of sum I want to place in the last row where same IDs are matching. And after all, delete those not required ones.
Appreciate in advance any help on this.
If you want to delete rows after summing then VBA is best. Otherwise you can achieve out using excel formulas only. First you need to extract unique value from first column. Then you need to sum values based on unique values you extracted. So if you have Office365 then use UNIQUE() function to extract unique values like below.
=UNIQUE(A1:A14)
Then use SUMIF() to sum for these unique values.
=SUMIF($A$2:$A$14,D2,$B$2:$B$14)
You can just use a Pivot Table.
Since you will be updating/refreshing the data:
Select a cell in your data table and Insert/Table
Then, depending on your Excel version: Summarize with Pivot Table (or similar)
Note that there is no need to sort the data.
Then you merely drag
ID to the Rows area
Value to the Values area
If your values are numeric, it should default to Sum of Values
Rename / Format / decide on totals and subtotals, etc.
To refresh, the data after changing it, you merely need to select Data/Refresh, or you can create a command button to do the same.

I need a macro to evaluate data in excel while counting by 10

I have data in 3 columns lets call them A, B, and C. All three columns are exclusively numbers. I need to look at column A and find all numbers that are between X-Y. For example I need all numbers between 95-104, these numbers can all be grouped as 100, I then need the next 10 so 105-114 and all those numbers can be group 110. Once I have the numbers in the group I need to find the average of group 100 in columns B and C. My final chart will read column A group, then B will be average of that group and C will be the other column average.
You can use simple loop.
with TAB1
for i = 1 to .Cells(.Rows.Count, "A").End(xlUp).Row
if cells(i,1) > Y and cells(i,1) < X then _
.EntireRow.Hidden = True
Else .EntireRow.Hidden = False
next
Then you can use subtotal excel function for average. Not tested it is little help I cant run excel on my mobile =)

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

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),"")

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

Resources