简体   繁体   English

Excel删除特定字符后,同时忽略该字符的前4个实例

[英]Excel remove after specific character while ignoring the first 4 instances of that character

I have a table that is formatted like so: 我有一个表,其格式如下:

New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS 纽约->阿尔巴尼->法律和金融->法律->律师

where -> is sub category. 其中->是子类别。

i want to limit the entire table to 5 sub-directories.. thus count 5-> then remove everything after that. 我想将整个表限制为5个子目录。因此,计数为5->然后删除所有内容。

ideas? 想法?

I guess best is to do a small user defined function in VBA to look for the nth occurence of the seperator string and keeps all that is left of that; 我猜最好是在VBA中执行一个小的用户定义函数,以查找分隔符字符串的第n次出现并保留所有剩余的内容。 example

Function CutAt(Arg As String, Sep As String, Level As Integer)
Dim Idx As Integer, ArgLen As Integer, SepLen As Integer, FCnt As Integer

    ArgLen = Len(Arg)                  ' argument length
    SepLen = Len(Sep)                  ' seperator length
                                       ' no check that arglen > seplen !!!
    Idx = 1                            ' position mark
    FCnt = 0                           ' find counter

    For Idx = 1 To ArgLen - SepLen + 1
        If Mid(Arg, Idx, SepLen) = Sep Then FCnt = FCnt + 1
        If FCnt = Level Then Exit For
    Next Idx

    If FCnt = Level Then
        CutAt = Left(Arg, Idx - 1)
    Else
        CutAt = Arg
    End If

End Function

Use this as a rather unprotected and unsafe peace of code just to demonstrate the principles - it's maybe not "enduser safe" in each & every way. 使用此代码作为一种不受保护的,不安全的代码和平方式只是为了演示其原理-可能不是每种方式都“最终用户安全”。 I tested with 我测试过

A1: New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS -> more -> even more -> much more
A2: New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS -> more -> even more
A3: New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS -> more
A4: New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS
A5: New York -> ALBANY -> Legal & Financial -> Legal
A6: New York -> ALBANY -> Legal & Financial

with formula =cutat(A1,"->",5) (copied down 6 times) I get 用公式=cutat(A1,"->",5) (向下复制6次)

New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS 
New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS 
New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS 
New York -> ALBANY -> Legal & Financial -> Legal -> ATTORNEYS
New York -> ALBANY -> Legal & Financial -> Legal
New York -> ALBANY -> Legal & Financial

you may need to add a trim function here & there if you want to remove leading/trailing blanks. 如果要删除前导/尾随空白,可能需要在此处和此处添加trim功能。

Hope that helps 希望能有所帮助

good luck 祝好运

MikeD's VBA solution is a very nice one. MikeD的VBA解决方案是一个非常好的解决方案。

For the record, here is a formula solution (thanks to this thread ) 作为记录,这里是一个公式解决方案(由于此线程

Put this formula to find the n-th occurence of a string in a new column: 将此公式放在新列中查找字符串的第n次出现:

=IF(LEN(B1)-LEN(SUBSTITUTE(B1,E1,""))>=F1*LEN(E1), SEARCH("@",SUBSTITUTE(B1,E1,"@",F1)),"Not Found")

where B1 houses a target string, E1 the character you are looking for ("->" in your case) and F1 the n-th occurrence (5 in your case). 其中B1包含目标字符串, E1包含您要查找的字符(在您的情况下为“->”), F1第n个出现的字符(在您的情况下为5)。

Afterwards, put this formula in another column: 然后,将此公式放在另一列中:

=IF(B6<>"Not Found",MID(B1,1,B6-LEN(E1)),B6)

where B6 is the result of the previous formula. 其中B6是上一个公式的结果。

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

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