Excel: Formula Returning from conditional - excel

How to return formula of other cell (column L) if choose item in column A.
Example:
if answer, then return =I5 & " " & J5
if foo, then return '=I7 & " " & J7 & " " & K7 & " x " & L7
I want to return the formula instead of the result.

Put this function in a public module
Function GetFormula(rng As Range) As Variant
GetFormula = rng.Formula
End Function
I think this is what you want. There are several options for how you want that formula returned such as R1C1 style.
Edit
Oh I think I get what you want more. Ok to apply the formula you have in your blue table to whatever cell you want in column F you can call the Indirect function.
So for example if you want to apply the formula '=I7 & " " & J7 & " " & K7 & " x " & L7 to your column F then call =INDIRECT(I7 & " " & J7 & " " & K7 & " x " & L7). In your column I don't include the = sign. it will make it a bit easier.
You can use Vlookup to find where this should be applied by using your lookup item in column A in your reference table (the blue table). Return the formula in column I.

You can achieve this by nesting your formulas.
put this in E5
=IF(A5 = "answer", A5 & " " & B5 & " " & D5,
IF(A5 = "bar", A5 & " " & B5 & " " & C5,
IF(A5 = "foo", A5 & " " & B5,
"No If Formula Here yet")))
I started the formula off with the first two formatting's for you.

Related

Remove comma from concatenated string for last cell in Excel

I have a table in Excel that has 10 columns. For each row there are different numbers of cells populated e.g.
My concatenated string is as follows for HTML emails
(" + Machine1 + ", " + Machine2 + ", " + Machine3 + ", " + Machine4 + ", " + Machine5 + ", " + Machine6 + ")
However I want the comma to not show after the last cell in the row (whether that be column 3,4,5,6).
This is what I get
(TEST1, TEST2, TEST3 , TEST4, TEST5, , )
I want to remove the two commas at the end. Hope this makes sense!
If you have Office 365 Excel then use TEXTJOIN:
="(" & TEXTJOIN(", ",TRUE,A2:F2) & ")"
If not then you will need to use IF for each return and Mid:
=MID("(" & IF(A2 <> "",", " & A2,"") & IF(A2 <> "",", " & B2,"") & IF(C2 <> "",", " & C2,"") & IF(D2 <> "",", " & D2,"") & IF(E2 <> "",", " & E2,"") & IF(F2 <> "",", " & F2,""),3,999)
Alternative without IF
Just in addition to Scott's valid solution, I demonstrate an approach via the REPT function. Applied on a cell containing a string, it "repeats" its content & comma once (indicated by COUNTA() equalling 1), whereas an empty cell results in a zero repetition which allows to omit not only the cell content but the comma, too:
="(" & SUBSTITUTE(REPT(A1&",",COUNTA(A1))&REPT(B1&",",COUNTA(B1))&REPT(C1&",",COUNTA(C1))&REPT(D1&",",COUNTA(D1))&REPT(E1&",",COUNTA(E1))&REPT(F1&",",COUNTA(F1))&"$",",$",")")
A simple SUBSTITUTE removes the last comma, wherever it occurs before the closing bracket ")".

Use cell value as string VBA

Cell A2 contains value "25-86".
I need cell B2 to have value "AJ 2586%"
This code reveals Syntax error:
Range("B2").Value = "AJ & Left(Range("A2"), 2) & Right(Range("A2"), 2) & %"
If I write it like that
Range("B2").Value = "AJ & Left(A2, 2) & Right(A2, 2) & %"
The functions Left and Right treat "A2" as string.
How could I extract a part of text from the cell and enter it in another cell?
The issue is that everything between the " " defines a string.
You can type your code like this:
Range("B2").Value = "AJ " & Left(Range("A2"), 2) & Right(Range("A2"), 2) & "%"
The following formula will return the value as a real percentage that you can use in calculations.
It just removes the - from the figure.
=SUBSTITUTE(A2,"-","")/100
Initially it will display the value as 25.86.
When you apply the custom number format of:
\AJ 0%
It will then display as AJ 2586%.
If that formula is in A1 then =A1/2 will return AJ 1293%.
Edit:
Just realised you want it in VBA:
Range("B2") = Replace(Range("A2"), "-", "") / 100
Range("B2").NumberFormat = "\AJ 0%"
Or, if you just want it as text:
Range("B2") = "AJ " & Replace(Range("A2"), "-", "") & "%"
Range("B2").Value = "AJ " + Left(Range("A2"), 2) + Right(Range("A2"), 2) + "%"
Indent code 4 spaces in order for it to be in the correct format

Excel function to combine cells and double quotes

I currently have a spreadsheet with the following format
A1 = <Name>
B1 = <Email>
C1 = New-MailContact
D1 = '-Name
E1 = '-ExternalEmailAddress
F1 = =(C1&" "&D1&" ""&A1&"" "&E1&" ""&B1&""")
My issue is that my F1 column results in the following output:
New-MailContact -Name "&A1&" -ExternalEmailAddress "&B1&"
Can someone please help me to fix the A1 and B1 records so they show up as the actual name and email rather than A1 and B1?
You have a bunch of extra quotes
=C1 & " " & D1 & " " & A1 & " " & E1 & " " & B1
should display the data concatenated.
If you want the name or other fields to be quoted, for every quote you want add two quotes in the string:
= """" & C1 & """"
will display New-MailContact in quotes, like this "New-MailContact"
See this as " "" " where the outside quotes are to denote it as a string, and the two quotes inside are for displaying the one "
So if you need name and email (A1 and B1) quoted, you need
=C1 & " " & D1 & " """ & A1 & """ " & E1 & " """ & B1 & """"
More examples.
The ASCII code for a double-quote character is 22 hex or 34 dec. The ASCII character code for a space is 20 hex or 32 dec.
=C1&CHAR(32)&D1&CHAR(32)&CHAR(34)&A1&CHAR(34)&CHAR(32)&E1&CHAR(32)&CHAR(34)&B1&CHAR(34)
'alternate with CONCATENATE
=CONCATENATE(C1, CHAR(32), D1, CHAR(32), CHAR(34), A1, CHAR(34), CHAR(32), E1, CHAR(32), CHAR(34), B1, CHAR(34))
'alternate with Office 365's TEXTJOIN¹
=TEXTJOIN(CHAR(32), FALSE, C1, D1, CHAR(34)&A1&CHAR(34), E1, CHAR(34)&B1&CHAR(34))
¹ The TEXTJOIN function was introduced with Excel 2016 with Office 365 or Excel Online.
        

Is there a way to quickly change the row number when doing = B1, = B2, ect. for thousands of rows?

This is what I am needing to put into Column A
= B1 & " " & C1 & " " & D1 & " " & E1 & " " & F1 & " " & G1 & " " & H1 & " " & I1
wherein the numbers will correspond to the current row that it is pasted in. Is there a faster way than simply pasting it in and manually changing the 1's to 2's, or to 3's or to 37892's?
You would string together a bunch of formulas:
=INDEX(B:B,Row()) & " " & INDEX(C:C,Row()) & ...
But if all you want is to put the formula in J1 and copy it down, then use your original formula. Put it in J1. then do one of the following:
Click on the lower right corner and drag down as far as you want.
Highlight J1 and all the cells below in which you want the formula. Go to Fill --> Down
Either of these will automatically change the row number on your original formula.

Paste values from 3 cells into one cell with delimiters in excel

I have the following data where A to C are the columns:
A B C
-5.274 -20.63 9.251
where each number is in a different cell.
I want to combine these numbers in the following way and paste them into a new cell (those of column D)
-5.274 (-20.63 − 9.251)
How can I do this?
You can't copy and paste them like that, but you can very easily:
D2: =A2 & " (" & B2 & " - " & C2 & ")"
To keep the formatting:
=TEXT(A1;"0.00")& "(" & TEXT(B1;"0.00") & " " & TEXT(C1;"0.00") & ")"

Resources