Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Visual Basic [игнор отключен] [закрыт для гостей] / Выделение текста от и до заданного тега (в Excel`e) / 3 сообщений из 3, страница 1 из 1
30.01.2007, 20:07
    #34294222
PersY
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Выделение текста от и до заданного тега (в Excel`e)
Нужна помощь, в макросах полный ноль ;(

Задача -- выделить жирным(курсивом) все что находиться среди двух заданных тегов.

Например задано: 1й тег -- <b> 2й тег -- </b>

Текст:
<b>Заголовок</b> текст другой текст и <b>еще выделено</b>.

Результат:
Заголовок текст другой текст и еще выделено .

Данная тема уже обсуждалась, но для Word
...
Рейтинг: 0 / 0
31.01.2007, 13:48
    #34296067
PersY
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Выделение текста от и до заданного тега (в Excel`e)
Задача решена частично следующим образом:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
Sub ReplaceTags(OpenTag As String, CloseTag As String)
    Dim x1 As Integer
    Dim x2 As Integer
    Dim s As String

    x1 = InStr(ActiveCell.Text, OpenTag)
    x2 = InStr(ActiveCell.Text, CloseTag)
    If x1 =  0  Or x2 =  0  Then Exit Sub
    s = Mid(ActiveCell.Text, x1 + Len(OpenTag), x2 - x1 - Len(OpenTag))
    
    ActiveCell.Value = Replace(ActiveCell.Text, OpenTag, "")
    ActiveCell.Value = Replace(ActiveCell.Text, CloseTag, "")
    
    If Mid(OpenTag,  2 ,  1 ) = "b" Then ActiveCell.Characters(x1, Len(s)).Font.Bold = True
    If Mid(OpenTag,  2 ,  1 ) = "i" Then ActiveCell.Characters(x1, Len(s)).Font.Italic = True
End Sub

Sub StartReplaceTags()
    Dim r As Range
    Dim firstadress As String
    
    Set r = Cells.Find(What:="<*>*</*>", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not r Is Nothing Then
        firstadress = r.Address
        Do
            r.Activate
            ReplaceTags "<b>", "</b>"
            ReplaceTags "<i>", "</i>"
            Set r = Cells.FindNext(r)
            If r Is Nothing Then Exit Do
        Loop While r.Address <> firstadress
    End If
End Sub

Но есть проблема, выделение происходит по одному тагу, по следующему просто сбрасывается.
Помогите закончить.
...
Рейтинг: 0 / 0
31.01.2007, 18:47
    #34297389
PersY
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Выделение текста от и до заданного тега (в Excel`e)
Всем спасибо :)
Решение:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
Sub ReplaceTags(OpenTag As String, CloseTag As String)
    Dim x1 As Integer
    Dim x2 As Integer
    Dim s As String

    x1 = InStr(ActiveCell.Text, OpenTag)
    x2 = InStr(ActiveCell.Text, CloseTag)
    If x1 =  0  Or x2 =  0  Then Exit Sub
    s = Mid(ActiveCell.Text, x1 + Len(OpenTag), x2 - x1 - Len(OpenTag))

    ActiveCell.Characters(x1, Len(OpenTag)).Delete
    ActiveCell.Characters(x2 - Len(OpenTag), Len(CloseTag)).Delete
    
    If Mid(OpenTag,  2 , Len(OpenTag) -  2 ) = "b" Then ActiveCell.Characters(x1, Len(s)).Font.Bold = True
    If Mid(OpenTag,  2 , Len(OpenTag) -  2 ) = "i" Then ActiveCell.Characters(x1, Len(s)).Font.Italic = True
    If Mid(OpenTag,  2 , Len(OpenTag) -  2 ) = "sup" Then ActiveCell.Characters(x1, Len(s)).Font.Superscript = True
    
End Sub

Sub StartReplaceTags()
    Dim r As Range
    Dim firstadress As String
    
    Set r = Cells.Find(What:="<*>*</*>", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not r Is Nothing Then
        firstadress = r.Address
        Do
            r.Activate
            ReplaceTags "<b>", "</b>"
            ReplaceTags "<i>", "</i>"
            ReplaceTags "<sup>", "</sup>"
            Set r = Cells.FindNext(r)
            If r Is Nothing Then Exit Do
        Loop While r.Address <> firstadress
    End If
End Sub


...
Рейтинг: 0 / 0
Форумы / Visual Basic [игнор отключен] [закрыт для гостей] / Выделение текста от и до заданного тега (в Excel`e) / 3 сообщений из 3, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]