Hi,
As requested – see here the explanation.
Note: I choose to explain the single formula solution instead of the formulae in comment no 2.
The example explained is
<MeterSerial>CWE90778</MeterSerial> assume that the aforesaid string is in Cell A1.
Our entire construction is
Code:
B1= TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"<","!"),">","!"),"/",""),"!",REPT(" ",LEN($A1))),COLUMN(A$1)*LEN($A1),LEN($A1)))
Lets take the TRIM function to the shelf as of now since, we are more concerned to know how does the MID function extract the data from the above string.
MID function returns the characters from the text string basis the starting position and length of characters.
Let’s look at its arguments:
MID(
text,start_num,num_chars)
So there are three arguments
- text: What is my input string(reference or hardcoded) in this case, it is
Code:
SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"<","!"),">","!"),"/",""),"!",REPT(" ",LEN($A1)))
- start_num: position in the string, i shall begin extracting from i.e. starting position. In this case
- num_chars: number of characters i wish retrieve from the string, in this case
We shall now go step by step and understand each argument for the example in question.
text argument:
In this argument, we replace all the unnecessary special characters first then insert a special character of my choice where ever required.
Confused – let us decipher it to understand what do I mean
Code:
SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"<","!"),">","!"),"/",""),"!",REPT(" ",LEN($A1)))
Once evaluated becomes
Code:
SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("<MeterSerial>CWE90778</MeterSerial>","<","!"),">","!"),"/",""),"!",REPT(" ",LEN($A1)))
So, we notice that it starts with the inner most SUBSTITUTE function and instructs it to replace less than character ”<” with an exclamation mark “!”. Once evaluated further the construction translates to
Code:
SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("!MeterSerial>CWE90778!/MeterSerial>",">","!"),"/",""),"!",REPT(" ",LEN($A1)))
Now, the construction is instructed as such that it passes the return of the inner most SUBSTITUTE function to second SUBSTITUTE function, where it substitutes the greater than character “>” to exclamation mark “!” Upon evaluating, it becomes
Code:
SUBSTITUTE(SUBSTITUTE("!MeterSerial!CWE90778!/MeterSerial!","/",""),"!",REPT(" ",LEN($A1)))
By Now, we have removed all the greater than and less than signs with exclamation mark. Furthermore, I pass on the return to the next SUBSTITUTE function to replace the forward slash”/” with nothing and making it look like
Code:
SUBSTITUTE("!MeterSerial!CWE90778!MeterSerial!","!",REPT(" ",LEN($A1)))
Assuming that, you know how REPT & LEN functions work
I further use LEN function to return the total length of the original text i.e. in this case 35
So,
Code:
REPT(“ “, LEN($A1))= REPT(“ “,35)
Now, we use the combination of SUBSTITUTE and REPT function and instruct the construction to replace each exclamation mark with 35 spaces( 35 is specific to this case, will vary from case to case) thus
Code:
SUBSTITUTE("!MeterSerial!CWE90778!MeterSerial!","!",REPT(" ",35))
translates to
Code:
" MeterSerial CWE90778 MeterSerial "
But, before I wind up the text argument, what do we notice here that every exclamation mark is replaced by 35 spaces.(remember)
So, this becomes our final string for the 1st argument of MID Function i.e. for text.
Now, is the second argument i.e. start_num
In this case, we definitely want to leave the first 35 blank spaces and then pick up text from the 36th character. Let us see our construction for start_num argument i.e.
We know LEN($A1)= 35
COLUMN(A$1) returns 1
Similarly, once COLUMN(A$1) is dragged one column towards the right it becomes COLUMN(B$1) and is equal to 2..
So and so forth
Again, back to
Code:
COLUMN(A$1)*LEN($A1)= 1*35 = 35
Phew!!
Let us talk about the 3rd argument i.e. num_chars
i.e. LEN($A1) = 35
If combined all the above
Code:
MID(" MeterSerial CWE90778 MeterSerial ",{35},35)
and once evaluated returns
{" MeterSerial "}
So we finally, wrap it into the TRIM Function to remove all the extra spaces..
Code:
TRIM({" MeterSerial "}) = “MeterSerial”
Similarly, when I drag it to the next column, in second argument of the MID function i.e COLUMN(B$1)*LEN($A1) becomes 2*35 = 70
So on and so forth..
Note: There is a huge scope to improve this construction, however, I did not bother much as it worked fine for all the examples mentioned in the initial post.
Hope this helps..!!