Start VBA macro when editing a cell

前端 未结 2 1260
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 23:43

I simply try to write a search macro in an excel sheet. How can I start a macro dynamically DURING editing a cell. When writing in a cell the search macro should run in the

相关标签:
2条回答
  • 2020-12-03 23:56

    Thanks to Dick Kusleika for answering my question and to put me on the right track.

    Here is the final solution for anybody having similar demands. It basically works with an ActiveX TextBox to enter the search-string. The macro than is looking in the search-area for all entries containing the search-string. All other filled rows within the search-field will get hidden. This works right away when writing into the TextBox. So, when deleting characters in the search-string the once hidden rows will appear right away if appropriate.

    Private Sub TextBox1_Change()
     Dim searchArea As Range, searchRow As Range, searchCell As Range
     Dim searchString As String
     Dim lastRow As Integer
    
     Application.ScreenUpdating = False
     searchString = "*" & LCase(TextBox1.Value) & "*"
    
     ' unhide rows to have the full search field when editing
     Rows.Hidden = False
    
     lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
     Set searchArea = Me.Range("A5", "A" & lastRow) 'Me.Range("A5").End(xlDown))
     searchArea.EntireRow.Hidden = True
    
     For Each searchRow In searchArea.Rows
       For Each searchCell In searchRow.Cells
         If LCase(searchCell) Like searchString Then
           searchRow.Hidden = False
           Exit For
         End If
       Next searchCell
     Next searchRow
    
     Application.Goto Cells(1), True
     Application.ScreenUpdating = True
    
    End Sub
    

    works like a charm.

    0 讨论(0)
  • 2020-12-04 00:17

    You can't. The code engine won't run while Excel is in Edit mode. You have to have the user enter the text in something other than a cell - like a control on the worksheet or a control on a userform.

    0 讨论(0)
提交回复
热议问题