Private gf As Boolean 'textbox1 gained focus--delete prior textbox value upon typing
Private ignoreChangeFocus As Boolean 'if true, overrides above...allows appending to textbox values on a doubleclick as well as accounting
'for focusing on A1 when already selected but was out of focus (textbox_lostfocus but no worksheet_selectionchange)
Private Sub TextBox1_GotFocus()
gf = False
If ignoreChangeFocus = False Then gf = True
ignoreChangeFocus = False
End Sub
Private Sub TextBox1_LostFocus()
If Selection.Address = Module1.celltolink Then 'when clicking on A1 when it was out of focus but still selected
ignoreChangeFocus = True
TextBox1.Activate
TextBox1.Value = ActiveSheet.Range(Module1.celltolink).Value
End If
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = False 'maintain default
If Target.Address = Module1.celltolink Then
Cancel = True
ignoreChangeFocus = True
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Module1.celltolink Then
TextBox1.Activate
End If
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
On Error Resume Next 'to ignore error and do nothing when attempt to move active cell off sheet
Dim rcelltolink As Range
Set rcelltolink = ActiveSheet.Range(Module1.celltolink)
Select Case KeyCode
Case vbKeyReturn, vbKeyEscape, vbKeyDown
Call exitTB(rcelltolink.Offset(1), ActiveSheet.Index)
Case vbKeyTab, vbKeyRight
Call exitTB(rcelltolink.Offset(, 1), ActiveSheet.Index)
Case vbKeyUp
Call exitTB(rcelltolink.Offset(-1), ActiveSheet.Index)
Case vbKeyLeft
Call exitTB(rcelltolink.Offset(, -1), ActiveSheet.Index)
Case vbKeyDelete
TextBox1.Value = ""
End Select
End Sub
Private Sub TextBox1_Change()
If gf Then
gf = False
TextBox1.Value = Right(TextBox1.Value, 1)
End If
updateFilter (TextBox1.Value)
End Sub
Private Sub updateFilter(Str)
If Len(Str) > 0 Then
With ThisWorkbook
.ActiveSheet.Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=1, Criteria1:="=" & Str & "*"
End With
Else: removeFilter
End If
End Sub
Private Sub removeFilter()
With ThisWorkbook
ActiveSheet.AutoFilterMode = False
End With
End Sub