/ / VBA Exibir várias instâncias de caracteres incorretos em uma planilha - excel, vba, excel-vba

VBA Exibe várias instâncias de caracteres ruins em uma planilha - excel, vba, excel-vba

Foi-me dado um pedaço de código usado para identificaraspas duplas etc. em uma planilha do excel. O código atual mostra uma instância de aspas duplas em uma caixa de mensagem e estou tentando fazer com que todas as instâncias sejam exibidas. Abaixo está um trecho do que recebi. Adicionei .address à variável para fornecer um endereço exato, mas mostra apenas uma. Tentei repetir a variável na esperança de exibir várias instâncias da citação, mas sem sorte até agora.

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

Respostas:

2 para resposta № 1

Você precisa encontrar o próximo certo para que Range.FindNext seja o que você está procurando. abaixo é como você pode fazer um loop e pegar todos os endereços

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