Excel VBA可以找到很多字符

时间:2016-03-31 15:26:22

标签: excel vba excel-vba

在Excel 2011 for Mac中,如何找到包含以下任何字符的下一个单元格:

ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÝ

(这些字符中的一个或多个将嵌入单元格中的其他字符串中,例如MálagaDesktop)

我会手动重新运行以找到下一个单元格,直到不再有匹配为止。

2 个答案:

答案 0 :(得分:3)

FIND()中的内置版本不支持此功能。

FIND类似,此UDF会返回其中任何一个字符#VALUE!

中第一次出现的位置

添加VBA模块并粘贴:

Public Function ContainsAccented(value As String) As Long
    Const chars As String = "ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ"
    If value Like "*[" & chars & "]*" Then
        For ContainsAccented = 1 To Len(value)
            If InStr(1, chars, Mid$(value, ContainsAccented, 1)) Then Exit Function
        Next
    End If
    ContainsAccented = CVErr(xlErrValue)
End Function

然后使用=ContainsAccented(A1)

答案 1 :(得分:1)

单击感兴趣的列中的单元格。重复运行此宏将使您沿着列向下查找每个所需字符:

Sub ytrewq()
    Dim s As String, r As Range, rng As Range, N As Long
    Dim v As String, CH As String, i As Long

    s = "ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ"
    N = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
    Set rng = Range(ActiveCell, Cells(N, ActiveCell.Column))

    For Each r In rng
        v = r.Text
        If v <> "" Then
            For i = 1 To Len(v)
                CH = Mid(v, i, 1)
                If InStr(s, CH) > 0 Then
                    r.Select
                    Exit Sub
                End If
            Next i
        End If
    Next r
    MsgBox "NO MORE CHARACTERS FOUND"
End Sub

修改#1:

此版本将涵盖所有UsedRange:

Public lastfound As Range

Sub ytrewq()
    Dim s As String, r As Range, rng As Range, start As Boolean
    Dim v As String, CH As String, i As Long, st As String

    s = "ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ"

    Set rng = ActiveSheet.UsedRange
    start = False
    On Error Resume Next
        st = lastfound.Address(0, 0)
    On Error GoTo 0
    If st = "" Then
        Set lastfound = rng(1)
    End If


    For Each r In rng
        If start Then
            v = r.Text
            If v <> "" Then
                For i = 1 To Len(v)
                    CH = Mid(v, i, 1)
                    If InStr(s, CH) > 0 Then
                        r.Select
                        Set lastfound = Selection
                        Exit Sub
                    End If
                Next i
            End If
        End If

        If r.Address(0, 0) = lastfound.Address(0, 0) Then
            start = True
        End If
    Next r
    MsgBox "NO MORE CHARACTERS FOUND"
End Sub

修改#2:

上述行为是在检测到 NOT FOUND 条件后挂断。以下版本(在最后添加一行代码)将允许整个事情从头重新开始:

Public lastfound As Range

Sub ytrewq()
    Dim s As String, r As Range, rng As Range, start As Boolean
    Dim v As String, CH As String, i As Long, st As String

    s = "ÀÁÂÃÄÅàáâãäåÈÉÊËèéêëÌÍÎÏìíîïÑñÒÓÔÕÖòóôõöÙÚÛÜùúûüÝýÿ"

    Set rng = ActiveSheet.UsedRange
    start = False
    On Error Resume Next
        st = lastfound.Address(0, 0)
    On Error GoTo 0
    If st = "" Then
        Set lastfound = rng(1)
    End If


    For Each r In rng
        If start Then
            v = r.Text
            If v <> "" Then
                For i = 1 To Len(v)
                    CH = Mid(v, i, 1)
                    If InStr(s, CH) > 0 Then
                        r.Select
                        Set lastfound = Selection
                        Exit Sub
                    End If
                Next i
            End If
        End If

        If r.Address(0, 0) = lastfound.Address(0, 0) Then
            start = True
        End If
    Next r
    MsgBox "NO MORE CHARACTERS FOUND"
    Set lastfound = Nothing
End Sub