添加一个命令,点击excel来searchhello

我试图添加一个search命令单击我的代码在工作表3上。我创build了一个命令单击,我试图从其他工作簿其他工作簿“单击”命令button,但它没有工作。 你能告诉我我做错了什么,我是相当新的vba,所以任何帮助将不胜感激

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 Dim iCount() As String Dim outws As Worksheet Dim Sheets As String Set ws = Worksheets("detail_report") Sheets("detail_report").Activate ActiveSheet.cmdRefresh_Click Set oRange = ws.Range("CZ:CZ") SearchString = "input" Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) 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 iCount = Split(FoundAt, ", ") Set outws = Worksheets("output") outws.Range("A2").Value = SearchString outws.Range("B2").Value = UBound(iCount) + 1 Else MsgBox SearchString & " not Found" End If End Sub 

您的IF条件可能不会像您喜欢的那样行事….尝试一个With … End With以获得速度/清晰度。

 With Worksheets("detail_report").Range("CZ:CZ") aCell = .Find(What:=SearchString, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)) If aCell <> "" Then 'this is probably what you want 'do your manipulation stuff here End if End With