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

在Excel工作表中,我得到了一段代码来识别双引号。 现在的代码显示了一个双引号的消息框中的一个实例,我试图让它显示所有的实例。 以下是我所得到的摘录。 我添加了.address给variables给一个确切的地址,但它只显示一个。 我试图重复希望显示引用的多个实例的variables,但目前为止没有运气。

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 

你需要find下一个正确的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