2014-10-03 106 views
0

我有一個包含大量選項卡的Excel 2013表單。每個選項卡都擁有我們公司擁有的不同軟件的許可證密鑰。然後將許可證綁定到安裝的計算機上。我正在編寫一個宏,列出找到搜索字符串的所有選項卡。如果我將輸入框留空,它將列出所有選項卡。但是如果我輸入一些東西來搜索它,它只會顯示宏消息的結尾。Excel VBA .find命令不適用於我

Sub TempMacro() 
    Dim ws As Worksheet 
    SearchListRow = 3 
    Dim rng As Range 
    Dim FindSting As String 

    FindString = InputBox("Enter a Search value") 

    For Each ws In ThisWorkbook.Sheets 

     'Set Search Function 
     Set rng = Cells.Find(What:=FindString, After:=ActiveCell, LookIn:=xlValues, _ 
     LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
     False, SearchFormat:=False) 

     If Not rng Is Nothing Then 
      'Record tab name where string was found 
      Sheets("LISTS").Range("O" & SearchListRow) = ws.Name 
      'incriment row 
      SearchListRow = SearchListRow + 1 
     Else 
      'MsgBox ("Nothing found in " & wb.Name) 
     End If 
    Next ws 
    MsgBox ("End Macro") 
End Sub 

回答

1

這是因爲您的單元格對象不完全合格。你在做什麼只是搜索activesheet。您需要Cells之前添加Ws,使其在這些表中搜索

試試這個

Set rng = Ws.Cells.Find(What:=FindString...... 

LookAt:=xlWhole不完全匹配。部分匹配,你將不得不使用xlPart

+0

這工作完美!我知道它必須是我失蹤的小事。非常感謝您的快速響應和幫助。 – 2014-10-03 18:31:22