Search code examples
vbaexceltrim

VBA, excel cut a part from a string


I would like to get a part from a string. Im not familiar with VBA or unusual excel functions, but I think maybe a macro or a VBA code can help me.

I need to cut down everything from the whole string from the cell, except what is after the Total. To clear this, I need the numbers which stand after the total. I dont know any functions in excel which can solve my problem. Is there something like trim or something which recognize the 'total' and give me the number after that?

Example: From this cell I need the 47 and the other 47

Cell: Appvg - Total: 47 GB - Free:20 Gb; rootvg - Total: 47 -Free: <1 GB>


Solution

  • Will the two Totals always be the same? If so, you can use this formula (just put this in a cell on the same row as your data, I am assuming your Cell is A1):

    =TRIM(MID(A2,SEARCH("total: ",A2)+LEN("total: "),2))

    If the GB numbers will ever be larger than two digits, change the "2" to "3". Do you have any other example cell data to work off to improve the above?

    Edit: If the numbers will be different, you can use this: =MID(A1,SEARCH("appvg - total: ",A1)+LEN("appvg - total: "),2)&", "&MID(A1,SEARCH("rootvg - total: ",A1)+LEN("rootvg - total: "),2)

    edit 2: I am working on the formula so if the GB are greater than two digits, such as 964 GB, to have a way to dynamically pick that out. This seems to work up to numbers 4 digits long: =MID(A4,SEARCH("appvg - total: ",A4)+LEN("appvg - total: "),SEARCH(" G",A4)-SEARCH(":",A4)-2)&", "&MID(A4,SEARCH("rootvg - total: ",A4)+LEN("rootvg - total: "),SEARCH(" G",A4,SEARCH(" G",A4)-SEARCH(":",A4)-1)-SEARCH(":",A4)-2)