2015-07-21 3 views
0

Я создал этот код для добавления найденных элементов, заключенных в «[]» или «()» или «{}». Если в моем слове документ у меня есть «ох!» [Плачет] Это больно! [Плачет] [смеется] », поэтому элементы, заключенные в« [] », будут добавлены в список, и их 3, но 2 одинаковы , Я хочу объединить их.
Как мне это сделать?Как удалить повторяющиеся элементы в списке

Sub cutsound() 
    Dim arrs, arrs2, c2 As Variant, pcnt, x2, x3, intItems as Integer 

    pcnt = ActiveDocument.Paragraphs.Count 
    arrs = Array("[", "(", "{") 
    arrs2 = Array("]", ")", "}") 
    UserForm1.Show False 
    Application.ScreenUpdating = False 
    With Selection 
     .WholeStory 
     For c2 = 0 To UBound(arrs) 
      .Find.Execute (arrs(c2)) 
      Do While .Find.Found 
       .MoveEndUntil Cset:=arrs2(c2), Count:=wdForward 
       .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
       UserForm1.ListBox1.AddItem Selection.Text 
       .MoveRight Unit:=wdCharacter, Count:=1 
       .EndKey Unit:=wdStory, Extend:=wdExtend 
       .Find.Execute 
      Loop 
     Next c2 
    End With 
    Application.ScreenUpdating = True 
End Sub 

ответ

0

Попробуйте объединить набор, а не список, он поддерживает дублирование.

+0

Как бы мне это сделать? Извините, чувак, я новичок в этом. –

0

Вы можете использовать ключи Dictionary для обеспечения уникальности. Добавить ссылку (Инструменты -> Ссылки ...) на Microsoft Scripting Runtime. Затем выполните следующие действия:

'I suggest searching using wildcards. The body of your loop will be much simpler 
Dim patterns(3) As String, pattern As Variant 
'Since these characters have special meaning in wildcards, they need a \ before them 
patterns(0) = "\[*\]" 
patterns(1) = "\(*\)" 
patterns(2) = "\{*\}" 

Dim rng As Range 'It's preferable to use a Range for blocks of text instead of Selection, 
       'unless you specifically want to change the selection 
Dim found As New Scripting.Dictionary 
For Each pattern In patterns 
    Set rng = ActiveDocument.Range 
    With rng 
     .WholeStory 
     .Find.Execute pattern, , , True 
     Do While .Find.found 
      found(rng.Text) = 1 'an arbitrary value 
      'If you want the number of times each text appears, the previous line could be modified 
      .Find.Execute 
     Loop 
    End With 
Next 

Dim key As Variant 
For Each key In found.Keys 
    Debug.Print key 
Next 

Примечание: этот код не будет находить записи в порядке их появления в документе, но первые записи с [], затем (), затем с {}.

Ссылки:

Смежные вопросы