简体   繁体   中英

creating formula in MS excel sheet to fetch value from row and column

I have a sheet that contains some data. every day we have to fetch values from it. [C3] [D3] [I1] is example of format we receive to fetch data. characters are columns and numbers are rows and so we have to match the required value manually and provide output. Can yo help me create formula so that I can paste " [C1] [B3] [E2]" - this format in a cell and receive the output in any other cell. I have attached a snap for the reference

Snap

The following formulas use FIND to look for the relative positions of each square bracket in the reference cell, you can then use LEFT and RIGHT to return the contents of each square bracket pair. Finally it uses INDIRECT to make Excel read the text string as a cell reference.

=INDIRECT(LEFT(RIGHT(H1,LEN(H1)-FIND("[",H1)),FIND("]",H1)-FIND("[",H1)-1))

=INDIRECT(LEFT(RIGHT(H1,LEN(H1)-FIND("[",H1,FIND("]",H1))),FIND("]",H1,FIND("[",H1,FIND("]",H1)))-FIND("[",H1,FIND("]",H1))-1))

=INDIRECT(LEFT(RIGHT(H1,LEN(H1)-FIND("[",H1,FIND("]",H1,FIND("[",H1,FIND("]",H1))))),FIND("]",H1,FIND("[",H1,FIND("]",H1,FIND("[",H1,FIND("]",H1)))))-FIND("[",H1,FIND("]",H1,FIND("[",H1,FIND("]",H1))))-1))

You can concatenate the results into one cell using the & sign, like this:

=INDIRECT(LEFT(RIGHT(H1,LEN(H1)-FIND("[",H1)),FIND("]",H1)-FIND("[",H1)-1))&INDIRECT(LEFT(RIGHT(H1,LEN(H1)-FIND("[",H1,FIND("]",H1))),FIND("]",H1,FIND("[",H1,FIND("]",H1)))-FIND("[",H1,FIND("]",H1))-1))&INDIRECT(LEFT(RIGHT(H1,LEN(H1)-FIND("[",H1,FIND("]",H1,FIND("[",H1,FIND("]",H1))))),FIND("]",H1,FIND("[",H1,FIND("]",H1,FIND("[",H1,FIND("]",H1)))))-FIND("[",H1,FIND("]",H1,FIND("[",H1,FIND("]",H1))))-1))

EDIT:

It might be easier to visualize what Excel is doing with SUBSTITUTE instead, in which you are able to specify which instance of a particular character you want to focus on. In the formula below, we substitute a particular left and right square bracket with the tilde sign for our FIND formula to look for, and then reference the substring between the two.

=MID(H1,FIND("~",SUBSTITUTE(H1,"[","~",1))+1,FIND("~",SUBSTITUTE(H1,"]","~",1))-FIND("~",SUBSTITUTE(H1,"[","~",1))-1)

That way, to find the second, third, n th cell reference, we just increment the instance number:

=MID(H1,FIND("~",SUBSTITUTE(H1,"[","~",2))+1,FIND("~",SUBSTITUTE(H1,"]","~",2))-FIND("~",SUBSTITUTE(H1,"[","~",2))-1)

=MID(H1,FIND("~",SUBSTITUTE(H1,"[","~",3))+1,FIND("~",SUBSTITUTE(H1,"]","~",3))-FIND("~",SUBSTITUTE(H1,"[","~",3))-1)

Again, you would wrap each of these in INDIRECT and then concatenate with & :

=INDIRECT(MID(H1,FIND("~",SUBSTITUTE(H1,"[","~",1))+1,FIND("~",SUBSTITUTE(H1,"]","~",1))-FIND("~",SUBSTITUTE(H1,"[","~",1))-1))&INDIRECT(MID(H1,FIND("~",SUBSTITUTE(H1,"[","~",2))+1,FIND("~",SUBSTITUTE(H1,"]","~",2))-FIND("~",SUBSTITUTE(H1,"[","~",2))-1))&INDIRECT(MID(H1,FIND("~",SUBSTITUTE(H1,"[","~",3))+1,FIND("~",SUBSTITUTE(H1,"]","~",3))-FIND("~",SUBSTITUTE(H1,"[","~",3))-1))

It isn't any shorter than the first formula (for now), but it's perhaps easier to understand the logic. More importantly, it's a lot easier to extend this version to include additional cell references, for example if H1 contained "[A1] [B12] [D32] [E4] [F19]" you just concatenate a couple more copies of the whole INDIRECT formula and increment the instance number for each one.

If vba works for you then this quick UDF will do it:

Function CONCATENATESPECIAL(rng As Range) As string
    Dim spArr() As String
    Dim i As Integer
    Dim temp As String
    temp = Replace(rng, "[", "")
    temp = Replace(temp, "]", "")
    spArr = Split(temp)
    For i = LBound(spArr) To UBound(spArr)
        CONCATENATESPECIAL = CONCATENATESPECIAL & Range(spArr(i))
    Next i
End Function

Put this in a module attached to the workbook:

在此处输入图片说明

Then you would simply use it as any other formula:

=CONCATENATESPECIAL(H1)

This will return a string. If you want to turn it into a number then:

=--CONCATENATESPECIAL(H1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM