2017-08-31 31 views
0

我被給了一段代碼,用於在Excel工作表中識別雙引號等。本代碼顯示消息框中雙引號的一個實例,我試圖讓它顯示所有實例。以下是我所得到的摘錄。我將.address添加到變量中以提供確切的地址,但僅顯示一個。我試圖重複希望顯示引用的多個實例的變量,但目前還沒有運氣。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