Copy part of a cell contents (with no clear seperator) - excel

I have a number of 'accounts' for which i would like to create a unique reference code for each. The reference code will be a combination of parts of different cells. So, for example, the reference for the 'account' occupying row 1 would be: cp78925
The cp part is a constant, and will always be the same.
The 789 part are the last three digits of cell A1, which contains a 10 digit code
The 25 part are the first two digits of cell B1, which contains the date on which the account was opened.
So for example:
if A1 = 1123456789, B1 = 25/10/2013 then the unique reference code in C1would be = cp78925
Searches on the internet show ways of separating the contents of cells by blanks (""), /, after letters etc., or making the last 3 digits BOLD/ITALIC, but I cant work out how to get my specific answer.
Thanks a lot in advance. I hope this is clear enough.

you need something like
="CP" & RIGHT(A1,3) & DAY(B1)
="CP" & RIGHT(A1,3) & TEXT(B1,"dd")
Anyway this formula is not going to give you an unique reference if two A1 code ends with the same 3 digits the same day.
1234567890 01/01/2013
3213512890 01/02/2013
Both will return you CP89001
Edit:
As reported by Sam092 (thanks), DAY() return a numeric value, TEXT() is the right function to use

Formula in C1
="CP"&RIGHT(A1,3)&LEFT(TEXT(B1,"DD/MM/YYYY"),2)
EDIT:
I see that you have tagged your question with VBA. You don't need vba for this but still if you want a VBA solution then try this
ThisWorkbook.Sheets("Sheet1").Range("C1").Formula = _
"=""CP""&RIGHT(A1,3)&LEFT(TEXT(B1,""DD/MM/YYYY""),2)"
or
ThisWorkbook.Sheets("Sheet1").Range("C1").Value = Evaluate( _
"=""CP""&RIGHT(A1,3)&LEFT(TEXT(B1,""DD/MM/YYYY""),2)")

Related

Excel cell text need to separate based on days and months

I have cell value with 115y300d which needs to be move to separate cell, however in few cell I have data like 10h30m, so it's mixed text.
What I want to do is value before "y" should go in Year Column "d" in Days, similar for h = hours and M in Minutes. Since it is not in similar format, I'm not able to do text to columns and other functions, and need your help.
You could use find() to do things like so:
=if(iferror(find("y",A2,1)>0,0),left(A2,find("y",A2,1)-1,"")
which will put the value before y into the cell or set it to blank.
Expand the idea to find d & y etc
One option could be:
Formula in B1:
=DROP(WRAPROWS(TEXTSPLIT(CONCAT(BYROW(A1:A5,LAMBDA(a,IF(RIGHT(a)="d",a&"hm","yd"&a)))),,{"y","d","h","m"},,""),4),-1)
If you hit CONCAT() limits, you can also do this by row (dragging):
=TEXTSPLIT(IF(RIGHT(A1)="d",A1&"hm","yd"&A1),{"y","d","h","m"},,,"")

Excel IF Function with LEFT Function criteria

I am using GOOGLE SHEETS and I am trying to manipulate received data in the following way:
In column A, I have a number. In column B I have a department. In column C I change the number to concatenate with the first letter of column B's value ONLY IF the number starts with 36. If it doesn't it simply returns the new number as the original number. I had to use the SEARCH function because IF LEFT
is not working here, so the issue is if changes the number wherever it finds a 36, instead of just those numbers starting with 36. Any assistance will be highly appreciated.
This formula is used in column C :
=IFERROR(IF(SEARCH("36",A2),CONCATENATE(A2,LEFT(B2,1)),A2),A2)
Here is the IF LEFT function that I've tried :
=IFERROR(IF(LEFT(A2,2) = "36",CONCATENATE(A2,LEFT(B2,1)),A2),A2)
(If errors occur or the old number doesn't start with 36 the new number should remain the original)
Thanks in advance
Edit: Changed LEFT function to correct ranges, forgot to edit this in initially when asking the question.
Give this a try:
=IF(LEFT(A2,2)="36",A2 & LEFT(B2,1),A2)
This approach does not use IFERROR() or CONCATENATE().It assumes your version of Excel uses the English version of the function names and uses , as the argument separator.
=IF(LEFT(A2,2)="36",CONCATENATE(A2,LEFT(B2,1)),A2)
=A2 & LEFT(B2, LEFT(A2, 2)="36")
This is a 'golfed' answer: made as short as possible (although, I have left the spaces in for readability)
If the first 2 characters of cell A2 are "36", then LEFT(A2, 2)="36" will be True. Otherwise, LEFT(A2, 2)="36" will be False.
Excel will treat True and False as 1 and 0 - so, the LEFT(B2, LEFT(A2, 2)="36") will take either the first character from cell B2, or no characters from cell B2, and add that to the end of the value from cell A2.

Reference a value (string) of a formula and check whether cells (sentences) contains the value

I am currently trying to create an Excel formula to check whether cells containing some sentences contains a specific word.
Tricky part is that I want to read the value of another formula for the word to be checked. Example below:
A B
1 Apple Amanda wants to buy some apple...
2 Orange The price of orange in current market...
Where the content of A1 came from a formula. I am trying to create a formula to check whether B1 contains A1.
And I currently have
Formula 1 = CELL("contents",A1) // To get cell content
Formula 2 = COUNTIF(B1:B2,"*Apple*") // To find whether "Apple" appears
But I cant combine these two formulas to create...
COUNTIF(B1:B2,"*CELL("contents",A1)*") // Results I am trying to get
Can any Excel wiz help me with this please? Ideally I am looking for a method without having to result to Macro.
Thanks
Maybe this works for you:
=COUNTIF(B1:B2,"* " & CELL("contents",A1) & " *")
Since you want Apple as a wildcard ("*Apple*"), you can wrap the result from first formula as a wildcard by "*" & formula & "*" and entering it in the second.
Why not =SEARCH(A1,B1)? If an error is returned, the value is not found.
=IFERROR(SEARCH(A1,B1),"Not found")
Or COUNTIF() with wildcards:
=COUNTIF(B1,"*"&A1&"*")

Excel, append one range to the end of another in one column

I have two columns of data in Excel. I would like to add a third column which combines the first and second. How can I do this with a formula such that I can add or remove data from columns A and B without ever having to touch column C?
Column A Column B Column C
Bob Mary Bob
Joe Melissa Joe
Jim Jackie Jim
Mary
Melissa
Jackie
The question explicit mention Microsoft Office Excel but I think would be good to add that if you are using Google Sheets a simpler solution is to use the curly brackets function/operator as mentioned by Lake at https://stackoverflow.com/a/14151000/1802726.
Here is a simple solution using FILTERXML and TEXTJOIN that can append MULTIPLE RANGES OF ANY SIZE, ARRAY FORMULAS AND REGULAR FORMULAS. Just replace YOUR_RANGES with the ranges or dynamic arrays you wish to join:
Simple version that ignores empty cells:
=FILTERXML("<A><B>" & TEXTJOIN("</B><B>",TRUE,YOUR_RANGES) & "</B></A>", "//B")
This one includes empty cells:
=IFERROR(FILTERXML("<A><B>" & TEXTJOIN("</B><B>",FALSE,YOUR_RANGE) & "</B></A>", "//B"), "")
If your input data contains the "<" character, the formulas above will return an error, so use this one instead:
=IFERROR(SUBSTITUTE(FILTERXML("<A><B>" & SUBSTITUTE(SUBSTITUTE(TEXTJOIN("ΨΨ",FALSE,YOUR_RANGE),"<","ЉЉ"),"ΨΨ","</B><B>")&"</B></A>","//B"),"ЉЉ","<"),"")
Note: you can change the FALSE to TRUE to ignore empty cells.
Note 2: You can replace the characters ЉЉ and ΨΨ by any character(s). I used these specific characters because it is very unlikely that your input data will contain ЉЉ or ΨΨ, which would cause errors.
NOTES:
Tested on:
Excel 365
EXAMPLE:
Using the simple version of the formula:
=FILTERXML("<A><B>" & TEXTJOIN("</B><B>",TRUE,A1:A3,B1:B3,C1:C3) & "</B></A>", "//B")
As a result you will get a dynamic array with the joined/appended ranges:
You can then apply any dynamic array formula (like UNIQUE) to the result.
HOW THIS WORKS:
The JOINTEXT function grabs your ranges and joins them as a text with the delimiter "</ B >< B >". Then, after adding "< A >< B >" to the beginning and "</ B ></ A >" to the end, we have an XML formatted text:
<A><B>1</B><B>2</B><B>3</B><B>A</B><B>B</B><B>C</B><B>!</B><B>#</B><B>#</B></A>
Finally, the FILTERXML will separate the tags into a dynamic array which will be the joined/appended ranges.
Enter the following formula into cell C1
=IF(ROW()>COUNTA(A:B),"",IF(ROW()<=COUNTA(A:A),INDEX(A:A,ROW()),INDEX(B:B,ROW()-COUNTA(B:B))))
Then copy down as far as you need.
Here's a nice way of interleaving the two rows.
In other words, turning this:
A X
B Y
C Z
into this:
X
A
Y
B
Z
C
Say the above table is in columns one and two, you'd do:
=IF(MOD(ROW(),2)=0,INDIRECT(ADDRESS(INT(ROW()/2), 1)),
INDIRECT(ADDRESS(INT(ROW()/2)+1, 2)))
Explanation
Let's break that down a little. The first part is MOD(ROW(), 2) which returns a zero if the current row is even, and a one if it's odd.
So the IF goes FALSE/TRUE/FALSE/TRUE as we go down the column.
Next, the ADDRESS(INT(ROW()/2), 1) returns us a string representation of the address of the cell at column 1 and at half the current row. (Rounded down). This piece on its own looks like:
#VALUE!
$A$1
$A$1
$A$2
$A$2
$A$3
$A$3
(That first #VALUE error is because 1/2 = 0.5 which rounds down to zero. There's no row zero!)
The INDIRECT function returns whatever value is found at that address.
The rest is pretty clear.
NOTE: There may be a slicker way than using INDIRECT and ADDRESS. Suggestions welcome.

Excel Problems- Calculated value as a cell reference

I'm relatively new to excel programming. I'm working on making a spread sheet that shows exponential decay. I have one column (A1:A1000) of 1000 random numbers between 1 & 10 using the TRUNC(RAND()*10,0) in each cell. The next Column (B1:B1000) has a logic mask =IF(A1=0,1,0) , where if the value in the A cell is 0, then the B cell shows a 1. Next, to find the number of 0's in the A column, I have the next column taking the sum of B1:B1000, which returns the number of 0's that showed up in the first column. I'm sure there's an easier way to do that, but this seems to work fine.
Here's my problem, hopefully it's clear what I'm asking:
Next, I want to take the sum of the logic column (B) from B1:B(1000- the value of the sum from (B1:1000)) in the cell below the cell that calculates sum(B1:B1000). Is there a way to to algebra in a cell formula to reference a cell? More simply, if I want to refer to A3, for example, is there a way to input something like A(2+1) to get A3? Does this make sense?
You can very easily do, in VBA:
ActiveCell.Formula = "=A" & (2+1) & "+15"
In Excel:
=INDIRECT(ADDRESS(2+1, COLUMN(A1)))+15
This will set the formula to "=A3+15". Generally, this is best done with variables, so remember to do that.
In Excel, and as pointed out by Eric, you can write the referance to the cells just like normal strings thanks to INDIRECT() function.
Just make sure that the string passed to INDIRECT() is a valid cell reference.
For example :
=SUM(INDIRECT("B" & 2+7*(H2-1)):INDIRECT("B"&(2+7*H2)-1))
Here, I sum 7 lines for each week (H2). It gives the sum of B2:B8, B9:B15, B16:B22, etc.
Hope my example will help you figure out how to use it in real situation.

Resources