删除“-”字之前的所有字符

时间:2019-10-14 10:25:26

标签: vba ms-word

我有一个很大的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

EDIT 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 :(得分: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