I need an excel formula that will extract the last four digits of a cell A2 and combine…
- User
I need an excel formula that will extract the last four digits of a cell A2 and combine them with the first two words of the cell in B2, but the data from the two cells should be put together with a hyphen. The space between the words in the second cell should be maintained.
Assistant
You can use the following formula to extract the last four digits of a cell A2 and combine them with the first two words of the cell in B2, separated by a hyphen:
=A2&"- "&LEFT(B2, LEN(B2) - (FIND(" ", B2) - 1))&RIGHT(B2, 4)Here's how the formula works:
A2&"- "&: This concatenates the contents of cell A2 with a hyphen and a space.LEFT(B2, LEN(B2) - (FIND(" ", B2) - 1))&: This extracts the first two words of the cell in B2, by finding the first space in the cell and returning the text to the left of that space. TheLEN(B2)function returns the length of the text in B2, and theFIND(" ", B2)function returns the position of the first space in the cell. By subtracting one from the position of the space, we ensure that the first two words are extracted. The resulting text is then concatenated with the hyphen and space that we added earlier.RIGHT(B2, 4)&": This extracts the last four digits of the cell in B2, by returning the text to the right of the last four digits. TheRIGHT(B2, 4)function returns the last four characters of the text in B2.
The formula assumes that the data in A2 and B2 is text, and that the last four digits of the text in B2 are numeric. If the data in B2 does not contain exactly four digits, the formula will return an error.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.