How to create a formula in result column based on the values of the previous column
if values in any previous columns (from c1 to c5) is greater than zero, I want to add it to the result.
If you happen to have access to TEXTJOIN function you can use the following:
Formula in F2:
=TEXTJOIN(",",TRUE,IF(A2:E2>0,$A$1:$E$1,""))
Note 1: It's an array formula and needs to be confirmed through CtrlShiftEnter
Note 2: Without access to TEXTJOIN this will become a much more painfull process, but for just the 5 columns you can try =IF(A2>0,A$1&",","")&IF(B2>0,B$1&",","")&IF(C2>0,C$1&",","")&IF(D2>0,D$1&",","")&IF(E2>0,E$1&",",""), however you'll have to expand to get rid of the trailing comma
If both the above options are not feasible, your best bet will be to write a UDF. For this exercise, you can write a rather simple one:
Public Function TextJoin2(rng1 As Range, rng2 As Range) As String
With Application
Dim arr As Variant: arr = .Evaluate("IF(" & rng1.Address & ">0," & rng2.Address & ","" "")")
TextJoin2 = Replace(.Trim(Join(arr, " ")), " ", ",")
End With
End Function
Call in F2 like: =TEXTJOIN2(A2:E2,A$1:E$1) and drag down.
You can use =CONCAT(IF(A2:E2>0;$A$1:$E$1&",";"")) as array formula, confirm with ShiftCtrlEnter.
Put this formula in F2 cell in your example. Expand that formula to whole range
What remains here is to remove comma on the end of result string.
That full formula would be like this
=LEFT(CONCAT(IF(A2:E2>0;$A$1:$E$1&",";""));LEN(CONCAT(IF(A2:E2>0;$A$1:$E$1&",";"")))-1)
Related
I got this type of sport activity data in excel:
I try to pull just a "calorie" value from column D to another cell.
Any suggestions how to pull this data?
Tried to do it by =MID function, but those strings are different length.
Any suggestions how to pull this data?
You could try a formula like as below,
• Formula used in cell E1
=TEXTAFTER(TEXTBEFORE(D1,",""count"),"calorie"":")+0
Alternative approach, If you are not using MS365, then use the formula as shown below
• Formula used in cell F1
=REPLACE(LEFT(D1,FIND("""count",D1)-2),1,FIND("calorie",D1)+8,"")+0
Or, In MS365 using CHOOSECOLS() & TEXTSPLIT()
• Formula used in cell G1
=CHOOSECOLS(TEXTSPLIT(D1,{":",","},,1),8)/1
=TEXTBEFORE(TEXTAFTER(A1,"""calorie"":"),",")
Using VBA, you can use the Split method, since the pattern is the same:
Sub ExtractCalorie()
Dim cel As String: cel = ActiveCell.Value
'in the active cell is the string: {"duration":67,"altitude":0.0,"distance":21499,"calorie":89000,"count":0,"floor":0,"steps":0}
Debug.Print Split(cel, ",")(3) 'it returns "calorie":89000
'If only the value needed:
Debug.Print Split(Split(cel, ",")(3), ":")(1) 'it returns 89000
End Sub
It can be easily transformed in a function, which works also as UDF, to be called from a formula in cell...
Is it possible to get all the values from Table 1 corresponding to the values in the cell of Table 2 without using VBA ?
For example.
In table 2 one value is "India Australia" (F3). I need to get the result as "IND AUS" looking from the table 1.
Thanks in advance!
Using TEXTJOIN:
=TEXTJOIN(" ",TRUE,VLOOKUP(FILTERXML("<a><b>"&SUBSTITUTE(F3," ","</b><b>")&"</b></a>","//b"),A:B,2,FALSE))
If one does not have TEXTJOIN then VBA is the only way to get it in one cell. See Here for a UDF that mimics TEXTJOIN: VLOOKUP with multiple criteria returning values in one cell
1] For Office 365 and Excel 2019 user, to use CONCAT function
In G3 array formula copied down :
=CONCAT(IF(ISNUMBER(SEARCH($A$3:$A$6,F3)),$B$3:$B$6,"")&" ")
This is an array formula and needs to be confirmed by pressing with Ctl + Shift + Enter.
2] For all Excel version user, try this longer formula
Create a range name
Select G3 >> Define name >>
Name : Abb
Refers to : =IF(ISNUMBER(SEARCH($A$3:$A$6,$F3)),$B$3:$B$6,"")
OK >> Finish
Then
In G3, enter formula and copied down :
=TRIM(IFERROR(INDEX(Abb,1),"")&" "&IFERROR(INDEX(Abb,2),"")&" "&IFERROR(INDEX(Abb,3),"")&" "&IFERROR(INDEX(Abb,4),"")&" "&IFERROR(INDEX(Abb,5),""))
I borrowed this VBA solution from extendoffice.com. It's very simple, easy to set up, and worked really well for my situation.
My scenario -
Sheet 1 contains the lookup value in column A where I want the formula results in column B.
Sheet 2 contains the table array in columns A and B where column A is again the lookup value and column B contains the values to be returned in the results.
First, press ALT+F11 to bring up the VBA editor.
Second, paste this Function into Module1 within the VBA editor:
Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)
'Updateby Extendoffice
Dim rng As Range
Dim xResult As String
xResult = ""
For Each rng In pWorkRng
If rng = pValue Then
xResult = xResult & ", " & rng.Offset(0, pIndex - 1) 'I changed EO's delimiter from a space to a comma+space.
End If
Next
If Left(xResult, 2) = ", " Then
xResult = Right(xResult, Len(xResult) - 2) 'This removes any leading commas that make it into the results.
Else
'All is well in the world!
End If
MYVLOOKUP = xResult
End Function
Third, double click on the cell you want the multi-results in and paste this formula.
=MYVLOOKUP(A2, 'SheetName'!A1:B1816, 2)
Fourth, edit the formula to fit your spreadsheet.
A2 - The lookup value that will be compared against the other list.
'SheetName'!A1:B1816 - Replace SheetName with the name of the sheet your other list is on and replace A1:B1816 with the full range of values that are both being looked up and returned.
2 - If the values you want returned in your results are in a column other than B, replace '2' with the column that contains the values you want returned in your results.
You're done! If you did everything correctly (and you don't have any Trust Center settings blocking VBA code) then it should look like a VLOOKUP returned multiple results in a single cell.
I need a solution for this:
A B
1. 1,2,3,4,5,,,
2.
3.
4.
5.
so i want to concatenate A column like this:
(A2;",";A3;",";A4;",";A5;",";A6;",";A7;","; and so on)
I want to remove commas (,) behind the number if they're less column - for example 5 and i concatenate 7.
How can i do that ?
there might be a better way of doing this but i will share my "dumb" way anyway.
In B1 i will make it = A1
In B2 =SUBSTITUTE(B1&","&A2,".","")
and i just need to fill down the column to the last row
you can get your final result at column B, last row
This is to prevent repetitive selecting all the cells in the whole list (using concatenate)
Use the function CONCATENATE as you said. You also can repeat that concatenate with the mouse for all rows.
=CONCATENATE(A2, ", ", A3) or =A2 & ", " & A3
For more information check this website:
Concatenate cells with a space, comma and other characters
I think, i find the simpliest & best solution and it's a vba code:
Function CONCATENATEMULTIPLE(Ref As Range, Separator As String) As String
Dim Cell As Range
Dim Result As String
For Each Cell In Ref
Result = Result & Cell.Value & Separator
Next Cell
CONCATENATEMULTIPLE = Left(Result, Len(Result) - 1)
End Function
Function: =CONCATENATEMULTIPLE(RANGE; ",")
CONCATENATE Excel Ranges (Using VBA)
I have 3 columns A,B,C and I need to merge the 3 columns and I have applied the forumala =A1&","&B1&","&C1 the output came as E column I need the output as D column.
The following formula will achieve your desired result:
=TEXTJOIN(",",TRUE,A1:C1)
Textjoin works like concatenate but can have a delimiter as an argument, also it gives you the ability to ignore blank cells, the first argument is the delimiter, the second is the flag to ignore blanks and the third is for the range.
As comments do mention that TEXTJOIN is only available for Office 365 subscribers, a possible alternative would be to build your UDF as below, this will allow you to use the formula above without an Office 365 subscription:
Function TEXTJOIN(delimiter As String, ignore_empty As Boolean, rng As Range) As String
Dim compiled As String
For Each cell In rng
If ignore_empty And IsEmpty(cell.Value) Then
'nothing
Else
compiled = compiled + IIf(compiled = "", "", delimiter) + CStr(cell.Value)
End If
Next
TEXTJOIN = compiled
End Function
Enter this formula into E1:
=CONCATENATE(A1, IF(AND(B1<>"", A1<>""), ",", ""), B1,
IF(AND(OR(A1<>"", B1<>""), C1<>""), ",", ""), C1)
Using TEXTJOIN might be a cleaner option, but is only available on more recent versions of Excel.
'Put this code in module and use formula concmulti and select the range
Function concmulti(slt As Range) As String
Dim str As String
Dim cell As Range
For Each cell In slt
str = str & cell.Value & ", "
Next cell
concmulti = str
End Function
If there are no spaces within the cells, then in D1 enter:
=SUBSTITUTE(TRIM(A1 & " " & B1 & " " & C1)," ",",")
and copy downwards:
I know I can concatenate a set of cells using & like this:
A1 & B1 & C1 & D1. Is there a way to write this for a range A1:P1 using & ?
You can accomplish this with a formula in a series of columns (each sequentially concatenating from the previous) or with a UDF (User Defined Function).
Formula Method
The formula in F2 is,
=A2
The formula in G2 is,
=IF(LEN(B2), F2&","&B2, F2)
Fill G2 right to catch all of the available text values.
VBA UDF Method
Tap Alt+F11 and when the VBE opens, immediately use the pull-down menus to Insert ► Module (Alt+I,M). Paste the following into the new pane titled something like Book1 - Module1 (Code).
Function udf_stitch_Together(r As Range, Optional d As String = ", ") As String
Dim s As String, c As Range
For Each c In r
If CBool(Len(c.Text)) Then _
s = s & IIf(Len(s), d, vbNullString) & c.Text
Next c
udf_stitch_Together = s
End Function
Tap Alt+Q to return to your worksheet.
The syntax is simply,
udf_stitch_Together(<range to concatenate>, <[optional] delimiter>)
For your purposes this would be,
=udf_stitch_Together(A2:P2)
... or,
=udf_stitch_Together(A2:P2, ", ")
Fill down as necessary. Note that I have used =udf_stitch_Together(A5:P5, ";") in F5:F6 to demonstrate the ability to change the delimiter.
For a 1D range as you have (a single column or row), you could try this
For a Row, your example, it would be called like
=StrCat(A1:P1, TRUE)
Function StrCat(rng1 As Range, bRows As Boolean) As String
If bRows Then
'row
StrCat = Join(Application.Transpose(Application.Transpose(rng1)), ", ")
Else
'column
StrCat = Join(Application.Transpose(rng1), ", ")
End If
End Function
I know this is an old question but I found a way to use Concatenate like #brettdj was talking about. Using the method from this link you can join together a range using a combination of Concatenate and Transpose like so:
Let say the cells you want to combine are in B2:B19.
In a blank cell, where you want to concatenate all the values type =CONCATENATE(TRANSPOSE(B2:B19))
Don’t press enter yet.
Select the TRANSPOSE(B2:B19) portion and press F9. This replaces the TRANSPOSE(B2:B19) with its result
Now remove curly brackets { and }
Press Enter & you're Done!
Demo from link:
(source: chandoo.org)