Comment trouver une valeur dans une colonne excel par des cellules de code vba.Trouver

je dois trouver une valeur celda dans une feuille Excel. J'ai été en utilisant ce code vba pour le trouver:

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

Le problème c'est quand je dois trouver la valeur seulement dans une colonne excel. Je le trouve avec le code suivant:

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

Mais je ne sais pas comment l'adapter à la premier code vba, parce que je dois utiliser la valeur nothing.

25
demandé sur Community 2013-02-18 11:58:36

3 réponses

il suffit d'utiliser

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
36
répondu Andrey Gordeev 2017-09-19 09:04:28

juste par souci d'exhaustivité, vous pouvez également utiliser la même technique ci-dessus avec les tableaux excel.

dans l'exemple ci-dessous, je regarde un texte dans n'importe quelle cellule D'une Table Excel nommée "tblConfig", place dans la feuille nommée Config qui est normalement réglée pour être cachée. J'accepte les valeurs par défaut de la méthode Find.

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
6
répondu Mário Meyrelles 2015-08-27 21:47:36
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
4
répondu Mona 2016-08-16 04:36:47