单元格文本处理——去冗余

目录

  • 去冗余
    • 说明
    • VBA源码
  • END

去冗余

说明

用vba写一个文本去冗余函数,第一个参数是待处理文本,之后的任意多个参数都是要剔除的冗余,执行时先对第一个参数的类型进行检验如果不是文本数据则返回"错误:非文本"

VBA源码

Function 去冗余(ByVal text As Variant, ParamArray redundancies() As Variant) As String
    Dim result As String
    Dim i As Long
    Dim redundancy As Variant
    
    ' 检查第一个参数是否为文本
    If Not IsString(text) Then
        去冗余 = "错误:非文本"
        Exit Function
    End If
    
    ' 初始化结果为原始文本
    result = text
    
    ' 遍历所有冗余内容并剔除
    For i = LBound(redundancies) To UBound(redundancies)
        redundancy = redundancies(i)
        If IsString(redundancy) Then
            result = Replace(result, redundancy, "")
        End If
    Next i
    
    ' 返回处理后的文本
    去冗余 = result
End Function

' 辅助函数:检查变量是否为文本类型
Function IsString(var As Variant) As Boolean
    IsString = VarType(var) = vbString
End Function

END

你可能感兴趣的:(javascript,开发语言,ecmascript)