VBA在工作表中显示多个错误字符实例

时间:2017-08-31 20:16:01

标签: excel vba excel-vba

在excel工作表中,我获得了一大堆用于识别双引号等的代码。本代码在消息框中显示了双引号的一个实例,我试图让它显示所有实例。以下是我所获得的摘录。我将.address添加到变量中以提供确切的地址,但它只显示一个。我试图重复变量希望显示引号的多个实例,但到目前为止没有运气。

Option Explicit
Sub BadCHARFinder()


'Finds any occurance of: Quotes (single or double, Underscore, Hyphen, Carot in the excel sheet
'Note: skips the header row when searching

'Find Double Quote (")

Dim foundDoubleQuote As Variant

Set foundDoubleQuote = ActiveSheet.Cells.Find("""", ActiveSheet.Cells(2, 1), xlValues, xlPart)

If (Not foundDoubleQuote Is Nothing) Then

    'found
    MsgBox "Found double quote at: " & foundDoubleQuote.Address, vbOKOnly, "foundDoubleQuote"
Else

    'not found

End If
End Sub

1 个答案:

答案 0 :(得分:2)

你需要找到下一个,所以Range.FindNext就是你要找的。下面是你如何循环和获取所有地址

Option Explicit
Sub BadCHARFinder()


'Finds any occurance of: Quotes (single or double, Underscore, Hyphen, Carot in the excel sheet
'Note: skips the header row when searching

'Find Double Quote (")

Dim foundDoubleQuote As Variant
Dim allCellAddresses As String
Dim firstCellAddress As String
Set foundDoubleQuote = ActiveSheet.Cells.Find("""", ActiveSheet.Cells(1, 1), xlValues, xlPart, xlByRows)

    If (Not foundDoubleQuote Is Nothing) Then
        'capture the first cell address or we end up in an endless loop
        firstCellAddress = foundDoubleQuote.Address
        Do
            'add the address of the cell to our string of cells
            allCellAddresses = allCellAddresses + vbCrLf + foundDoubleQuote.Address
            'find the next cell with the data
            Set foundDoubleQuote = ActiveSheet.Cells.FindNext(foundDoubleQuote)
        'keep going until we find the first address we started with
        Loop While foundDoubleQuote.Address <> firstCellAddress
        'inform user
        MsgBox "Found double quote at: " & allCellAddresses, vbOKOnly, "foundDoubleQuote"
    Else

        'not found

    End If
End Sub