Удалить все символы перед словом "-" - PullRequest
0 голосов
/ 14 октября 2019

У меня большой документ Word со многими разными именами, например:

Joe Bloggs Ltd - Joe Bloggs Limited Acc
Adam Smith Ltd - Adam Smith Limited Acc

Все они находятся в таблицах, в отдельных ячейках.

Мне было интересно, каким будет код VBA в слове, чтобы получить вывод, подобный следующему:

Joe Bloggs Limited Acc
Adam Smith Limited Acc

Вместо того, чтобы вручную удалять деталь перед '-' каждый раз.

Любая помощь будет высоко ценится.

Редактировать:

У меня пока что это не работает:

Sub replaceCharToNothing()
Dim itable As Table
Dim C As Cell
Dim str1 As String
For Each itable In ThisDocument.Tables
    For Each C In itable.Range.Cells
        C.Range.Text = Left(str1, InStr(str1, "-") - 1)
    Next
Next
End Sub

РЕДАКТИРОВАТЬ 2.0

Теперь у меня есть код, который работает:

Dim myTable As Table
Dim myrow As Row
Dim i As Integer
Dim x As Integer

For Each myTable In ActiveDocument.Tables
    For Each myrow In myTable.Rows
        For i = 1 To myrow.Cells.Count
                x = InStr(myrow.Cells(i).Range.Text, "-")
            If x > 0 Then
                    myrow.Cells(i).Range = Replace(Trim(Mid(myrow.Cells(i).Range.Text, x + 1)), ChrW(13), "")
            End If
        Next i
    Next myrow
Next myTable

1 Ответ

0 голосов
/ 15 октября 2019
Dim myTable As Table
Dim myrow As Row
Dim i As Integer
Dim x As Integer

For Each myTable In ActiveDocument.Tables
    For Each myrow In myTable.Rows
        For i = 1 To myrow.Cells.Count
            x = InStr(myrow.Cells(i).Range.Text, "-")
            If x > 0 Then
                myrow.Cells(i).Range = Replace(Trim(Mid(myrow.Cells(i).Range.Text, x + 1)), ChrW(13), "")
            End If
        Next i
    Next myrow
Next myTable
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...