2013-02-18 16 views
22

Devo trovare un valore celda in un foglio Excel. Stavo usando questo codice VBA per trovarlo:Come trovare un valore in una colonna excel tramite codice vba Cells.Find

Set cell = Cells.Find(What:=celda, After:=ActiveCell, LookIn:= _ 
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False) 


If cell Is Nothing Then 
    'do it something 

Else 
    'do it another thing 
End If 

Il problema è quando devo trovare il valore solo in una colonna di Excel. Trovo con codice successivo:

Columns("B:B").Select 
    Selection.Find(What:="VA22GU1", After:=ActiveCell, LookIn:=xlFormulas, _ 
     LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False).Activate 

Ma io non so come adattarla al primo codice VBA, perché devo utilizzare il valore nothing.

+6

http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/ Inoltre si prega di evitare l'uso di '.Select' Vedi questo link http: //stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select/10718179#10718179 –

+0

Se vuoi semplicemente sapere se il valore esiste da qualche parte nell'intervallo, è un'esecuzione più rapida (vale la pena se si controllano centinaia di valori) per utilizzare una formula di Excel. Se ad esempio celda è un numero, è possibile utilizzare IF Evaluate ("COUNTIF (Sheet1! A1: A1000," & celda & ")")> 0 THEN ... – lessthanideal

risposta

32

Basta usare

Columns("B:B").Select 
Set cell = Selection.Find(What:="celda", After:=ActiveCell, LookIn:=xlFormulas, _ 
     LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
     MatchCase:=False, SearchFormat:=False) 

If cell Is Nothing Then 
    'do it something 

Else 
    'do it another thing 
End If 
4

Solo per amor di completezza, è anche possibile utilizzare la stessa tecnica di cui sopra con tabelle di Excel.

Nell'esempio seguente, sto cercando un testo in qualsiasi cella di una tabella di Excel denominata "tblConfig", posto nel foglio denominato Config che normalmente è impostato per essere nascosto. Accetto le impostazioni predefinite del metodo Trova.

Dim list As ListObject 
Dim config As Worksheet 
Dim cell as Range 


Set config = Sheets("Config") 
Set list = config.ListObjects("tblConfig") 

'search in any cell of the data range of excel table 
Set cell = list.DataBodyRange.Find(searchTerm) 

If cell Is Nothing Then 
    'when information is not found 
Else 
    'when information is found 
End If 
3
Dim strFirstAddress As String 
Dim searchlast As Range 
Dim search As Range 

Set search = ActiveSheet.Range("A1:A100") 
Set searchlast = search.Cells(search.Cells.Count) 

Set rngFindValue = ActiveSheet.Range("A1:A100").Find(Text, searchlast, xlValues) 
If Not rngFindValue Is Nothing Then 
    strFirstAddress = rngFindValue.Address 
    Do 
    Set rngFindValue = search.FindNext(rngFindValue) 
    Loop Until rngFindValue.Address = strFirstAddress 
Problemi correlati