简体   繁体   中英

How do I remove after a specific text and before a number, then delete duplicates using VBA?

So I'm trying to create a VBA Code where all you have to do is push a button and it automatically cuts a text string specifically and deletes duplicates then shows the results in another column.

For example of what I'm looking to accomplish, in Column A will be the text string and in Column B will be the result:

A

TEST_TESTING_COMPUTER_1x1_1x1_TEST_111111_MN_2222222_VVVVV_WWW_GGGG_LLLL_TEXTFORMAT1x1

The result I want to accomplish is:

B

111111_MN_2222222_VVVVV_WWW_GGGG_LLLL_TEXTFORMAT

And if another item in B is exactly like the result above it would automatically search for all duplicates and delete them. There doesn't need to be any blank spaces in B everything can be moved right underneath each other.

The Column A will always have the same amount of underscores but the 1x1 can change to 50x300 , 700x700 , 200x23 , etc. I've figured out how to go about deleting everything prior to the 6th _ but haven't put it into VBA format. I'm also at a loss of how I would go about deleting the very end portion as it does vary. The code I have to delete everything prior is:

=RIGHT(SUBSTITUTE(A1,"_",CHAR(10),6),LEN(A1)-FIND(CHAR(10),SUBSTITUTE(A1,"_",CHAR(10),6),1)+1)

The number of rows that Column A could have anywhere near 5000 to 15000 lines.

Any help would be much appreciated!

EDIT: To help understand fully what I'm trying to accomplish below is an image of what I would like it to look like: 例子

This is purely an example but essentially what I am looking to get to. Where Column A would be pasted in, (Has the same number of _ every time in Column A) But varies at the last portion of the ###x### or ##x### or ###x## or #x# , and what I would like to do is add in a button that anyone is able to press, it automatically looks at A, then grabs everything to the right of the 6th _ , and grab everything prior to the numbers at the end. Then once that is done delete duplicates so the end result would look like this once everything is said and done:

示例_2

This would be the end result that I'm trying to accomplish. Hopefully this explains a little bit more of what I'm trying to do. Let me know if more explanation is needed.

Use the underscore as the delimiter for the Split() function and process the array. https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/split-function

This will take some refinement, but it's basically going to look like this:

Sub FixThatString()
    Dim arText() As String
    Dim sInput As String
    Dim i As Integer
    Dim sOutput as String

    sInput = "TEST_TESTING_COMPUTER_1x1_1x1_TEST_111111_MN_2222222_VVVVV_WWW_GGGG_LLLL_TEXTFORMAT1x1"

    arText = Split(sInput, "_")

    'Looking to end with something like this - 111111_MN_2222222_VVVVV_WWW_GGGG_LLLL_TEXTFORMAT
    For i = LBound(arText()) To UBound(arText())
        If arText(i) = "TEST" Or arText(i) = "TESTING" Or arText(i) = "COMPUTER" Or arText(i) = "1x1" Then
            arText(i) = ""
        Else
            'Do nothing 
        End If
    Next i

    'Put back together & put the underscore we stripped out back in.
    For i = LBound(arText()) To UBound(arText()) - 1
        If arText(i) <> "" Then '"" else arText(i) & "_"
            sOutput = sOutput & arText(i) & "_"
        End If
    Next i
    'The last part of the string doesn't need an underscore, so just add it w/out it.
    sOutput = sOutput & arText(i + 1)

End Sub

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