2013-02-18 14 views
22

Excelのシートに値celdaがあります。私はそれを見つけるために、このVBAコードを使用していました:vbaコードでExcelの列の値を見つける方法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 

私はExcelの列に値を見つけるために持っているときに問題があります。私は次のコードでそれを見つける:

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

しかし、私は値nothingを使用する必要があるため、最初のVBAコードに適合するのか分かりません。

+6

http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/また、このリンクhttpを参照してください.Select' '使用は避けてください。 //stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select/10718179#10718179 –

+0

値が範囲のどこかに存在するかどうかを知りたいのであれば、より速く実行することができます値)を使用してExcel式を使用します。例えば、セルダが数字の場合、IF Evaluate( "COUNTIF(Sheet1!A1:A1000、"&celda& ")")> 0 THENを使用できます。 – lessthanideal

答えて

32

ただ、ただ、完全を期すために、あなたはまた、Excelのテーブルと上記と同じ手法を使用することができます

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

を使用しています。

以下の例では、 "tblConfig"という名前のExcelテーブルの任意のセルでテキストを探していますが、Configという名前のシートには通常非表示に設定されています。私は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 
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 
関連する問題