2017-08-31 3 views
0

Excelワークシートで二重引用符などを識別するためのコードが与えられました。現在のコードは、メッセージボックスに二重引用符のインスタンスを1つ表示し、すべてのインスタンスを表示するようにしようとしています。以下は私が与えられたものの抜粋です。 .addressを変数に追加すると正確なアドレスが得られますが、1つしか表示されません。私は変数を繰り返して、引用符の複数のインスタンスを表示することを期待しましたが、これまでの運はありません。VBAワークシートに複数の不正な文字のインスタンスを表示

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 
+0

実際に達成しようとしているのは何ですか? – SJR

答えて

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 
関連する問題