【问题标题】:Adding a command click to excel to search for hello添加命令 click to excel 搜索 hello
【发布时间】:2016-05-21 00:48:51
【问题描述】:

我正在尝试将搜索命令单击添加到工作表 3 上的代码中。我创建了命令单击并尝试了 "Clicking" Command Button from other workbook 其他工作簿,但它没有用。你能告诉我我做错了什么吗? 我对 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

【问题讨论】:

  • "...但它不起作用。"它怎么行不通?你有错误吗?宏是否运行,但没有任何反应?请描述它如何不像您预期​​的那样工作。
  • 它运行但没有任何反应
  • 删除您的 On Error 行,运行它,看看它是否中断。然后让我们知道它停止时突出显示的行。
  • 您发布的代码无法编译,因为标签Err 未定义。
  • @Scott - 你在哪里看到错误行?还是被删掉了?

标签: vba excel highlighting


【解决方案1】:

您的 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

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多