2012-02-20 94 views
0

我正在使用下面的代碼來查找列。VBA搜索標準

Set FinColumn = .Find(What:="Tax", AFter:=.Cells(1, 1), LookIn:=xlValues, LookAt _ 
      :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
      False, SearchFormat:=False). 

如果我有像下面的列,它應該確定。

Tax  Tax&Fee Tax&Fee1 

我怎樣才能改變上面的設置語句找到所有上述columns.is有任何搜索條件,我可以實現。

感謝,

Chaitu

回答

2

雖然我的第一個答案是正確的,它是不完整的。以下代碼循環查找每個包含「稅」的單元格,並在全部處理完畢後停止。

直接回答你的問題是用xlPart代替xlWhole。但是,有些錯誤會阻止您的代碼工作;例如,您沒有定義查找操作的範圍。我在.Find之前添加了.Cells

希望這會有所幫助。

Option Explicit 
Sub FindAllTax() 

    Dim ColCrnt As Long 
    Dim ColLast As Long 
    Dim FinColumn As Range 
    Dim RowCrnt As Long 
    Dim RowLast As Long 

    RowLast = 0 
    ColLast = 0 

    With Sheets("Sheet6") 

    Set FinColumn = .Cells.Find(What:="Tax", After:=.Cells(1, 1), _ 
      LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _ 
      SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 

    Do While True 
     If FinColumn Is Nothing Then 
     ' No occurrence of "Tax" found 
     Exit Do 
     End If 
     RowCrnt = FinColumn.Row 
     ColCrnt = FinColumn.Column 
     If RowCrnt < RowLast Or _ 
     (RowCrnt = RowLast And ColCrnt < ColLast) Then 
     ' Current cell is above last cell so have looped after finding 
     ' all values. 
     Exit Do 
     End If 
     Debug.Print "Cells(" & RowCrnt & ", " & ColCrnt & ")=" & _ 
              .Cells(RowCrnt, ColCrnt).Value 
     RowLast = RowCrnt 
     ColLast = ColCrnt 
     Set FinColumn = .Cells.FindNext(FinColumn) 
    Loop 
    End With 

    Debug.Print "All cells containing ""tax"" processed" 

End Sub 
+0

+1在第一去工作! :) – 2012-02-21 03:18:11

1

託尼已經給你一種方法。這是另一個使用通配符。現在,使用通配符,因爲假設你有細胞是非常重要的,其中

A1 =含稅

B10 =含稅&費

C15 =含稅& FEE1

D20 = 123Tax

G45 = DoggyTax

如果你只是想尋找稅*即稅單,繳稅&手續費和稅金&費用1?

而且當你正在做的所有細胞中的搜索,那麼你必須指定範圍。這裏是快速示例

Option Explicit 

Sub Sample() 
    Dim oRange As Range, aCell As Range, bCell As Range 
    Dim ws As Worksheet 
    Dim ExitLoop As Boolean 
    Dim SearchString As String, FoundAt As String 

    On Error GoTo Err 

    '~~> The Sheet where the search has to be performed 
    Set ws = Worksheets("Sheet1") 
    '~~> In All cells 
    Set oRange = ws.Cells 

    '~~> Search string 
    SearchString = "Tax*" 

    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _ 
       LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
       MatchCase:=False, SearchFormat:=False) 

    '~~> If search was found 
    If Not aCell Is Nothing Then 
     Set bCell = aCell 
     FoundAt = aCell.Address 
     Do While ExitLoop = False 
      Set aCell = oRange.FindNext(After:=aCell) 

      If Not aCell Is Nothing Then 
       If aCell.Address = bCell.Address Then Exit Do 
       FoundAt = FoundAt & ", " & aCell.Address 
      Else 
       ExitLoop = True 
      End If 
     Loop 
    Else 
     MsgBox SearchString & " not Found" 
    End If 

    MsgBox "The Search String has been found these locations: " & FoundAt 
    Exit Sub 
Err: 
    MsgBox Err.Description 
End Sub 

您可以在下面提到的鏈接中找到更多關於FIND()和FINDNEXT()的信息。

話題:.Find和.FindNext在Excel VBA

LINKhttp://siddharthrout.wordpress.com/2011/07/14/find-and-findnext-in-excel-vba/

注意:如果你想找到的 「稅」 的所有實例,那麼你不需要通配符。所有你需要做的就是按照Tony的建議使用下面的內容。

LookAt:=xlPart 

HTH

希德