Макрос Word для изменения цвета для отрицательных чисел в определенном столбце в зависимости от значения в другом столбце - PullRequest
0 голосов
/ 26 октября 2018

Заранее спасибо за любой ответ.
Я работаю над презентацией некоторых докладов. Периодические отчеты импортируются из другого программного обеспечения в шаблон Word. Для всех таблиц и для каждой строки я хотел бы изменить цвет отрицательных чисел в столбце 14, только если в столбце 3 есть определенный текст. К сожалению, я должен использовать шаблон Word, чтобы сделать это. Кажется, что макрос - это мой единственный вариант, поэтому я попытался Франкенштейну кое-что из разных макросов, которые я нашел в Интернете:

Dim varColumn As Column
Dim clColumn As Column
Dim cCell As Variant

    Set clColumn = Selection.Columns(3)
    Set varColumn = Selection.Columns(14)

With clColumn
With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = "value"
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        If .Information(wdWithInTable) = True Then
        Selection.MoveRight Unit:=wdCell, Count:=11
        End If
            If cCell < 0 Then
            Selection.Font.color = wdColorRed
        End If
    Loop
    End With
End Sub

Ответы [ 2 ]

0 голосов
/ 28 октября 2018

Попробуйте:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "-[0-9][0-9,.]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found = True
    If .Information(wdWithInTable) = True Then
      If .Cells(1).ColumnIndex = 14 Then
        If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then
          .Font.ColorIndex = wdRed
        End If
      End If
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

Если в таблице могут быть вертикально объединенные ячейки, измените:

If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then

на:

If Split(.Tables(1).Cell(.Cells(1).RowIndex, 3).Range.Text, vbCr)(0) = "specified text" Then
0 голосов
/ 27 октября 2018

Я думаю, что макрос нуждается в строках, чтобы повторить поиск.Смотрите две строки, добавленные перед циклом.

With Selection
    .HomeKey Unit:=wdStory 'Starts at the beginning, to search all tables.
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "value"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
    End With
    Do While .Find.Found
        If .Information(wdWithInTable) = True And _
            .Cells(1).ColumnIndex = 3 Then 'Confirms it's in the 3rd column.
            .MoveRight Unit:=wdCell, Count:=11
        End If
        If .Range < 0 Then
            .Font.Color = wdColorRed
        End If
        .Collapse wdCollapseEnd 'Collapses the selection to no characters.
        .Find.Execute 'Searches again from the current selection point.
    Loop
End With
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...