如何在Excel上删除某些语言的单词(在我的例子中是中文)?

时间:2018-02-20 15:26:33

标签: excel excel-vba vba

我想要的是从Sheet中删除所有中文文本:

问题样本:

enter image description here

1 个答案:

答案 0 :(得分:2)

这可能不是达到你想要的最佳方式,但是你可以遍历每个单元格中的字符,如果它们不属于[A到Z]或[a到z]或[0 to 9]用空格替换字符,然后修剪额外的空格。

修改

根据cybernetic.nomad的评论,我还在ASCII查找中包含了逗号,撇号和句点:

Sub foo()
Dim i As Long, x As Long, LastRow As Long
Dim StrVal As String
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row
'get the last row with data on Column I, amend to whichever column you are using

For i = 1 To LastRow 'loop from row 1 to last on Column I
    For x = 1 To Len(ws.Cells(i, "I")) 'loop through characters in each cell
        StrVal = Mid(ws.Cells(i, "I"), x, 1)
        If IsLetter(StrVal) = False Then 'check if character is valid in English
            ws.Cells(i, "I") = Replace(ws.Cells(i, "I"), StrVal, " ") 'if not replace with a space
        End If
    Next x
    ws.Cells(i, "I") = Trim(ws.Cells(i, "I")) 'trim extra spaces from cell
Next i
End Sub

Function IsLetter(strValue As String) As Boolean
'function to check whether character is between [A-Z], [a-z], [0-9], Comma, Apostrophe and Periods.
        Select Case Asc(strValue)
            Case 65 To 90, 97 To 122, 48 To 57, 39, 44 To 46 
                IsLetter = True
            Case Else
                IsLetter = False
        End Select
End Function