I would like to do something like this (Formula to find the header index of the first non blank cell of a range in Excel?) except that I want to capture all the nonblank cells.
An application of what I am expecting would produce column "prod"
2 3 5 7 11 13 | prod |
2 1 2^1
3 1 3^1
4 2 2^2
5 1 5^1
6 1 1 2^1 3^1
7 1 7^1
8 3 2^3
9 2 3^2
10 1 1 2^1 5^1
11 1 11^1
12 2 1 2^2 3^1
13 1 13^1
14 1 1 2^1 7^1
15 1 1 3^1 5^1
16 4 2^4
I wouldn't mind a result with multiple separators ie. 6= 2^1*3^1**** , as they could be removed.
This user defined function will stitch together the header value with the range value.
In a standard public module code sheet:
Option Explicit
Function prodJoin(rng As Range, hdr As Range, _
Optional op As String = "^", _
Optional delim As String = " ")
Dim tmp As String, i As Long
For i = 1 To rng.Count
If Not IsEmpty(rng.Cells(i)) Then
tmp = tmp & delim & hdr.Cells(i).Text & op & rng.Cells(i).Text
End If
Next i
prodJoin = Mid(tmp, Len(delim) + 1)
End Function
On the worksheet as,
If you absolutely must use worksheet functions then stitch 6 conditional concatenations together.
=TRIM(IF(B2, B$1&"^"&B2&" ", TEXT(,))&
IF(C2, C$1&"^"&C2&" ", TEXT(,))&
IF(D2, D$1&"^"&D2&" ", TEXT(,))&
IF(E2, E$1&"^"&E2&" ", TEXT(,))&
IF(F2, F$1&"^"&F2&" ", TEXT(,))&
IF(G2, G$1&"^"&G2&" ", TEXT(,)))
Related
I have the following data
X f1 f2 f3
1 20 20/5/2 3
2 0 10/5/0 7
3 15 20/2/1 3
4 30 80/0/9 3
I want to make SUM() of all values in f2 column but it gives me an error because of the /.
How can I take each value, separately?
Plus, how to get each relative percentage of each cell in f2? For example, the first cell of f2 would be 74,07 / 18,52 / 7,41 taken from doing (20/27 - 5/27- 2/27)
use:
=INDEX(SUM(IFERROR(SPLIT(F1:F; "/"); 0)*1))
update:
=ARRAYFORMULA(IF(C1:C="";;SUBSTITUTE(FLATTEN(QUERY(TRANSPOSE(ROUND(
IFERROR(SPLIT(C1:C; "/")/MMULT(1*IFERROR(SPLIT(C1:C; "/"));
SEQUENCE(COLUMNS(SPLIT(C1:C; "/")); 1;;)^0))*100; 2));;9^9)); " "; " / ")))
I have the following sheet.
H I J K ... BD
2,3 4 2,4,7 ... 1
3,7 10 ... 8,13
The cell ‘H’ has 2,3 as text but cell ’I’ has the number 4, cell ‘J’ has the 2,4,7 as text and cell ‘BD’ has 1 as a number. All the cells that have 2 or more numbers as text are separated by commas
I want to sum all numbers and non-numbers to one single row using array-formula.
The result must be
BE
16
41
The range is between H2:BD with H1:BD1 for the headers
I have used this code:
=arrayformula(if(row(A1:A)=1;"BottleQty";if(len(A1:A)>0;
SUBSTITUTE(transpose(query(transpose(H1:BD);;COLUMNS(H2:BD)));",";"");iferror(1/0))))
but the result is
BE
2 3 4 2 4 7 1
3 7 10 8 13
Any help ??
Thanks in advance
=ARRAYFORMULA(IF(LEN(TRIM(TRANSPOSE(QUERY(TRANSPOSE(A2:D),,99^99)))),
MMULT(IFERROR(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(A2:D),,99^99)), ", ")*1, 0), ROW(INDIRECT("A1:A"&
COLUMNS(IFERROR(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(A2:D),,99^99)), ", ")*1, 0))))^0), ))
After I apply a filter in A1 I get a result like:
A B C D
Uno 1 3 1
Uno 1 2 2
Tres 2 2 3
I want to replace "Uno" for "Dos" in the same cells.
A B C D
Dos 1 3 1
Dos 1 2 2
Tres 2 2 3
I have seen SUBSTITUTE() and REPLACE() functions, but, they put the replacement in another cells.
I have tried:
A1: SUBSTITUTE(A1:A)
try:
=ARRAYFORMULA(REGEXREPLACE(IFERROR(FILTER(K:N, L:L="ok"), " "), "Uno", "Dos"))
REGEXREPLACE(range, "replace this", "to this")
ARRAYFORMULA - for continuity
=ARRAYFORMULA(REGEXREPLACE(REGEXREPLACE(IFERROR(FILTER(K:N, L:L="ok"), " "),
"Uno", "Dos"), "Tres", "Cuatro")
I am trying to take input from user in an array .And want to remove duplicate elements but the result is weird .I don't have to use uniq or any other ruby method.Here is my code
digits = []
digits = gets.chomp.to_i
k= digits & digits
puts k
input - 1 2 3 4 1 2 3 <br>
Required output- 1 2 3 4<br>
Getting output 1
gets.chomp returns string "1 2 3 4 1 2 3"
Then you call to_i on that string:
"1 2 3 4 1 2 3".to_i => 1
Consequentially 1 & 1 => 1
You should do this:
digits = gets.chomp.split(' ').map(&:to_i)
k = digits & digits
puts k
I want to make a script that takes any number, counts up to them and returns them in a format.
so like this
for i = 1,9 do
print(i)
end
will return
1
2
3
4
5
6
7
8
9
however I want it to print like this
1 2 3
4 5 6
7 8 9
and I want it to work even with things more than 9 so things like 20 would be like this
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20
I'm sure it can be done using the string library in lua but I am not sure how to use that library.
Any help?
function f(n,per_line)
per_line = per_line or 3
for i = 1,n do
io.write(i,'\t')
if i % per_line == 0 then io.write('\n') end
end
end
f(9)
f(20)
The for loop takes an optional third step:
for i = 1, 9, 3 do
print(string.format("%d %d %d", i, i + 1, i + 2))
end
I can think of 2 ways to do this:
local NUMBER = 20
local str = {}
for i=1,NUMBER-3,3 do
table.insert(str,i.." "..i+1 .." "..i+2)
end
local left = {}
for i=NUMBER-NUMBER%3+1,NUMBER do
table.insert(left,i)
end
str = table.concat(str,"\n").."\n"..table.concat(left," ")
And another one using gsub:
local NUMBER = 20
local str = {}
for i=1,NUMBER do
str[i] = i
end
-- Makes "1 2 3 4 ..."
str = table.concat(str," ")
-- Divides it per 3 numbers
-- "%d+ %d+ %d+" matches 3 numbers divided by spaces
-- (You can replace the spaces (including in concat) with "\t")
-- The (...) capture allows us to get those numbers as %1
-- The "%s?" at the end is to remove any trailing whitespace
-- (Else each line would be "N N N " instead of "N N N")
-- (Using the '?' as the last triplet might not have a space)
-- ^ e.g. NUMBER = 6 would make it end with "4 5 6"
-- The "%1\n" just gets us our numbers back and adds a newline
str = str:gsub("(%d+ %d+ %d+)%s?","%1\n")
print(str)
I've benchmarked both code snippets. The upper one is a tiny bit faster, although the difference is almost nothing:
Benchmarked using 10000 interations
NUMBER 20 20 20 100 100
Upper 256 ms 276 ms 260 ms 1129 ms 1114 ms
Lower 284 ms 280 ms 282 ms 1266 ms 1228 ms
Use a temporary table to contain the values until you print them:
local temp = {}
local cols = 3
for i = 1,9 do
if #temp == cols then
print(table.unpack(temp))
temp = {}
end
temp[#temp + 1] = i
end
--Last minute check for leftovers
if #temp > 0 then
print(table.unpack(temp))
end
temp = nil