简体   繁体   English

VBA,Excel从字符串中截取一部分

[英]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. 我不熟悉VBA或异常的excel函数,但是我认为也许宏或VBA代码可以为我提供帮助。

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. 我不知道excel中的任何功能都可以解决我的问题。 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 示例:在这个单元格中,我需要47和其他47

Cell: Appvg - Total: 47 GB - Free:20 Gb; 单元: Appvg-总计:47 GB-空闲:20 Gb; rootvg - Total: 47 -Free: <1 GB> rootvg-总计:47-免费:<1 GB>

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): 如果是这样,您可以使用以下公式(只需将其与数据放在同一行的单元格中,假设您的单元格为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". 如果GB号码大于两位数,请将“ 2”更改为“ 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) 编辑:如果数字不同,则可以使用以下代码: =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. 编辑2:我正在研究公式,因此如果GB大于两位数(例如964 GB),则可以有一种方法来动态地选择出来。 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) 这似乎可以处理最多4位数字: =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)

If you want a true VBA solution, here's a function to get to at least the first number (and a similar one can be created to get to the second number): 如果您想要一个真正的VBA解决方案,则可以使用以下函数至少获取第一个数字(可以创建一个相似的函数以获取第二个数字):

Function first_number(a_string As String) As Double
    'assumes one space after each "Total:" occurrence
    loc_of_first_total = InStr(1, a_string, "Total")
    end_of_first_number = InStr(loc_of_first_total + 7, a_string, " ") - 1
    first_number = CDbl(Mid(a_string, loc_of_first_total + 7, end_of_first_number - loc_of_first_total - 6))
End Function

If you're not familiar with InStr, a Google search would probably get to a better explanation than I could provide. 如果您不熟悉InStr,那么对Google搜索的解释可能会比我提供的更好。 All the numbers I used (eg, 7, 1, 6) are just the math of where the number will be if you assume that "Total:" plus one space exists before each number needed. 我使用的所有数字(例如7、1、6)仅是假设您假设“总计:”加一个空格(在每个需要的数字之前)时该数字所处位置的数学公式。 I wasn't sure if decimals could exist, so I returned a Double, but it could be an Integer or Long depending on what you expect in the data. 我不确定小数是否可以存在,因此我返回了Double,但是根据您对数据的期望,它可以是Integer或Long。

For the first instance: (where WordToFind might be "Total:" 对于第一个实例:(其中WordToFind可能是“总计:”

=LOOKUP(2,1/MID(A1,SEARCH(WordToFind,A1)+LEN(WordToFind)+1,ROW(INDIRECT("1:5"))),MID(A1,SEARCH(WordToFind,A1)+LEN(WordToFind)+1,ROW(INDIRECT("1:5"))))

For the 2nd instance: 对于第二个实例:

=LOOKUP(2,1/MID(A1,SEARCH(WordToFind,A1,SEARCH(WordToFind,A1)+1)+LEN(WordToFind)+1,ROW(INDIRECT("1:5"))),MID(A1,SEARCH(WordToFind,A1,SEARCH(WordToFind,A1)+1)+LEN(WordToFind)+1,ROW(INDIRECT("1:5"))))

If you need more instances, VBA might be a better option. 如果需要更多实例,则VBA可能是一个更好的选择。 If your number portion might be longer than 5 characters, change the "5" in "1:5" 如果您的数字部分可能超过5个字符,请在“ 1:5”中更改“ 5”

I assume that you need the results in two separated cells. 我假设您需要将结果放在两个单独的单元格中。 Also note that the key string to search for is "Total:" 另请注意,要搜索的键字符串“总计:”

Cell A11 has the following value (use different totals as to validate the different results) 单元格A11具有以下值(使用不同的总数来验证不同的结果)

Appvg - Total: 147 GB - Free:20 Gb; Appvg-总计:147 GB-免费:20 Gb; rootvg - Total: 247 -Free: <1 GB> rootvg-总计:247-免费:<1 GB>

Use this formula to obtain the Total: 147 (GB) 使用此公式可获得总计:147 (GB)

=LEFT(SUBSTITUTE(A11,LEFT(A11,LEN("Total:")+SEARCH("Total:",A11)),""),
-1+SEARCH(" ",SUBSTITUTE(A11,LEFT(A11,LEN("Total:")+SEARCH("Total:",A11)),"")))

Use this formula to obtain the Total: 247 (-Free) 使用此公式可获得总计:247 (-免费)

=LEFT(SUBSTITUTE(A11,LEFT(A11,
LEN("Total:")+SEARCH("|",SUBSTITUTE(A11,"Total:","|",2))),""),
-1+SEARCH(" ",SUBSTITUTE(A11,
LEFT(A11,LEN("Total:")+SEARCH("|",SUBSTITUTE(A11,"Total:","|",2))),"")))

In case the key string changes, a cell can be use to enter the key strings to search. 万一密钥字符串发生更改,可以使用一个单元格输入要搜索的密钥字符串 If this is the case use these formulas: 如果是这种情况,请使用以下公式:

The key string "Total:" is entered in cell A12 在单元格A12中输入关键字字符串 “总计:”

Use this formula to obtain the Total: 147 (GB) 使用此公式可获得总计:147 (GB)

=LEFT(SUBSTITUTE(A11,LEFT(A11,LEN(A12)+SEARCH(A12,A11)),""),
-1+SEARCH(" ",SUBSTITUTE(A11,LEFT(A11,LEN(A12)+SEARCH(A12,A11)),"")))

Use this formula to obtain the Total: 247 (-Free) 使用此公式可获得总计:247 (-免费)

=LEFT(SUBSTITUTE(A11,LEFT(A11,
LEN(A12)+SEARCH("|",SUBSTITUTE(A11,A12,"|",2))),""),
-1+SEARCH(" ",SUBSTITUTE(A11,LEFT(A11,
LEN(A12)+SEARCH("|",SUBSTITUTE(A11,A12,"|",2))),"")))

All formulas work regardless of the length of the numbers 所有公式均有效,无论数字的长度如何

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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