How to delete all non-Chinese characters or numbers or letters in the text in all cells of a certain column in excel?

1. Brief introduction

  When we have to deal with a large number of rows of excel data, sometimes the cell content in some rows in a column contains some parts we don't want (such as containing some special characters, numbers, letters, etc.), this time we want to put these Delete the unwanted part. There is no related function in Excel that can solve this problem for us. We can only write a macro function to realize this function. The following is a macro function with complete functions that can solve this problem. You can directly Copy the code to your own file, or you can download the file containing this macro function and use it. The macro file contains multiple commonly used macro functions and has detailed introductions. You can use them as needed.
 Download link: https://download.csdn.net/download/u014541881/88036953
 If you have special functional requirements, please contact me privately.

2. Function and parameter introduction

(1) Function

 It can be used to judge whether the selected cell has a special character of the specified class, delete it if it has, and return the deleted cell content, and turn the font of the selected cell containing the special character of the specified class red; if not, return it as it is, and do not Change the font color of the selected cell.

(2) Parameter introduction

  rng is a required parameter, representing the selected cell.
  charType is a required parameter, representing the type of special characters to be queried, notchin represents only judging whether the selected cell contains special characters other than Chinese characters, num represents only judging whether the selected cell contains special characters such as numbers, letter represents only Determine whether the selected cell contains special letters of the alphabet (including uppercase and lowercase letters); cus represents which special character you specify, only to determine whether the selected cell contains the specified special character, the specified special character is in the third Input a parameter customChars, such as ",;!" means only to judge whether the selected cell contains comma, seal mark, exclamation mark these three special, note that the third function parameter is an optional parameter, only in the second parameter The third parameter needs to be input only when it is cus. If it contains special characters of the type defined by the parameters, delete these special characters in the selected cells one by one, return the processed cell content, and turn the font of the selected cells containing the specified type of special characters into red; if not If it contains special characters of the type defined by the parameter, the cell content will be returned as it is, and the font color of the selected cell will not be changed.

3. Complete VBA code

Function CheckAndRemoveSpecialChars(ByVal rng As Range, ByVal charType As String, Optional ByVal customChars As String = "") As String
    On Error Resume Next ' 忽略可能的错误
    
    Dim cellValue As String
    Dim specialChars As String
    Dim i As Integer
    
    ' 检查参数是否有效
    If rng.Cells.Count > 1 Then
        CheckAndRemoveSpecialChars = "只能选择单个单元格作为参数。"
        Exit Function
    End If
    
    cellValue = rng.Value
    specialChars = ""
    
    ' 判断特殊字符类型
    Select Case charType
        Case "notchin"
            specialChars = "!@#$%^&*()-_=+[]{}|;':,.<>/?`~"
            specialChars = specialChars & GetNonChineseChars()
        Case "num"
            specialChars = "0123456789"
        Case "letter"
            specialChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Case "cus"
            specialChars = customChars
        Case Else
            CheckAndRemoveSpecialChars = "输入的第二个参数为无效的特殊字符类型。"
            Exit Function
    End Select
    
    ' 删除特殊字符并返回处理后的单元格内容
    For i = 1 To Len(specialChars)
        cellValue = Replace(cellValue, Mid(specialChars, i, 1), "")
    Next i
    
   If cellValue <> rng.Value Then
        ' 解除单元格保护
        rng.Locked = False
    
         ' 设置字体为红色
         On Error Resume Next ' 忽略可能的错误
         rng.Font.Color = RGB(255, 0, 0)
         On Error GoTo 0 ' 恢复默认错误处理
    
          ' 上锁保护单元格
          rng.Locked = True
   End If
    
    CheckAndRemoveSpecialChars = cellValue
End Function

Function GetNonChineseChars() As String
    Dim nonChineseChars As String
    Dim i As Integer
    
    nonChineseChars = ""
    
    ' 添加非中文字符范围
    For i = 32 To 126
        If Not IsChineseCharacter(ChrW(i)) Then
            nonChineseChars = nonChineseChars & ChrW(i)
        End If
    Next i
    
    GetNonChineseChars = nonChineseChars
End Function

Function IsChineseCharacter(ByVal character As String) As Boolean
    Dim code As Long
    
    code = AscW(character)
    IsChineseCharacter = (code >= &H4E00 And code <= &H9FFF) Or (code >= &H3400 And code <= &H4DBF)
End Function

4. How to use the macro code

The specific usage method can be modeled on the article How to judge whether a cell in a column contains special characters (such as spaces, numbers, underscores, etc.), and return the special characters

5. Rendering

insert image description here

Guess you like

Origin blog.csdn.net/u014541881/article/details/131665183