Guest User

Untitled

a guest
Jan 5th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. '============================================================================================
  2. ' Module : ThisWorkbook
  3. ' Version : 1.0
  4. ' Part : 1 of 1
  5. ' References : N/A
  6. ' Source : https://superuser.com/a/1283003/763880
  7. '============================================================================================
  8. Option Explicit
  9.  
  10. Private Const s_FuzzyLookupResultsTable As String = "MatchingTable"
  11. Private Const RESTORE_SELECTION As Boolean = True
  12.  
  13. Private Sub Workbook_SheetChange _
  14. ( _
  15. ByVal TheWorksheet As Object, _
  16. ByVal Target As Range _
  17. )
  18. Dim Ä As Excel.Application: Set Ä = Excel.Application
  19. Dim ƒ As Excel.WorksheetFunction: Set ƒ = Excel.WorksheetFunction
  20.  
  21. Const l_FuzzyLookup_AddIn_Undo_Sheet As String = "FuzzyLookup_AddIn_Undo_Sheet"
  22. Const s_InCell_Error_Message As String = "SAVE, CLOSE & REOPEN if pressing GO again doesn't fix it"
  23.  
  24. Static swkstActiveFuzzyLookupSheet As Worksheet
  25. Static sstrOriginalSelection As String
  26.  
  27. Select Case True
  28. Case TheWorksheet.Name <> l_FuzzyLookup_AddIn_Undo_Sheet And swkstActiveFuzzyLookupSheet Is Nothing:
  29. Exit Sub
  30. Case TheWorksheet.Name = l_FuzzyLookup_AddIn_Undo_Sheet And swkstActiveFuzzyLookupSheet Is Nothing:
  31. 'TODO If missing table
  32. Set swkstActiveFuzzyLookupSheet = ActiveSheet
  33. sstrOriginalSelection = Selection.Address
  34. Case TheWorksheet.Name = l_FuzzyLookup_AddIn_Undo_Sheet And Not swkstActiveFuzzyLookupSheet Is Nothing:
  35. With swkstActiveFuzzyLookupSheet.ListObjects(s_FuzzyLookupResultsTable)
  36. Ä.EnableEvents = False
  37. ' This is a Fuzzy Lookup bug work-around to show an in-cell error if the output doesn't update
  38. If .ListColumns.Count > 1 Then
  39. Dim strHeaderRowRange As String: strHeaderRowRange = .HeaderRowRange.Address
  40. Dim varHeaders() As Variant: varHeaders = ƒ.Transpose(ƒ.Transpose(.HeaderRowRange.Value2))
  41. With Range(.ListColumns(2).DataBodyRange, .ListColumns(.ListColumns.Count).DataBodyRange)
  42. Dim strDeletedRange As String: strDeletedRange = .Address
  43. .Delete
  44. End With
  45. Range(strDeletedRange).Insert Shift:=xlToRight
  46. Range(strDeletedRange).Value2 = s_InCell_Error_Message
  47. Range(strHeaderRowRange).Value2 = varHeaders
  48. End If
  49. ' This is the magic line that forces the output back into the table
  50. .HeaderRowRange.Cells(1).Select
  51. Ä.EnableEvents = True
  52. End With
  53. Case TheWorksheet.Name = swkstActiveFuzzyLookupSheet.Name:
  54. With swkstActiveFuzzyLookupSheet.ListObjects(s_FuzzyLookupResultsTable).Range
  55. If Target.Cells(Target.Cells.Count).Address = .Cells(.Cells.Count).Address Then
  56. ' <optional>
  57. ' Only restore the selection if set to do so and the selection is not the first header cell
  58. If RESTORE_SELECTION _
  59. And sstrOriginalSelection <> .Cells(1).Address _
  60. Then
  61. Ä.EnableEvents = False
  62. Range(sstrOriginalSelection).Select
  63. Ä.EnableEvents = True
  64. ' Unfortunately the above Select doesn't stick. The Add-in trys to change the selection another 1 or 2 times.
  65. ' The following hack is required so that the Workbook_SheetSelectionChange handler can revert these attempts.
  66. ' Note that if the original selection contains the first header cell, only 1 attempt is made. Otherwise it makes 2 attempts.
  67. RevertSelection _
  68. RevertTo:=Selection, _
  69. NumberOfTimes:=IIf(Intersect(Selection, .Cells(1)) Is Nothing, 2, 1)
  70. End If
  71. ' </optional>
  72. sstrOriginalSelection = vbNullString
  73. Set swkstActiveFuzzyLookupSheet = Nothing
  74. End If
  75. End With
  76. Case Else:
  77. Exit Sub
  78. 'End Cases
  79. End Select
  80.  
  81. End Sub
  82.  
  83. ' The following code is only needed if the RESTORE_SELECTION option is required.
  84. ' If the code is removed, the optional code in the Workbook_SheetChange handler above also needs to be removed.
  85.  
  86. Private Sub RevertSelectionIfRequired()
  87. RevertSelection
  88. End Sub
  89.  
  90. Private Sub RevertSelection _
  91. ( _
  92. Optional ByRef RevertTo As Range, _
  93. Optional ByRef NumberOfTimes As Long _
  94. )
  95.  
  96. Static srngRevertTo As Range
  97. Static slngRevertCount As Long
  98.  
  99. Select Case True
  100. Case Not RevertTo Is Nothing:
  101. Set srngRevertTo = RevertTo
  102. slngRevertCount = NumberOfTimes
  103. Case Not srngRevertTo Is Nothing:
  104. With Application
  105. .EnableEvents = False
  106. srngRevertTo.Select
  107. .EnableEvents = True
  108. End With
  109. slngRevertCount = slngRevertCount - 1
  110. If slngRevertCount = 0 Then Set srngRevertTo = Nothing
  111. Case Else:
  112. Exit Sub
  113. 'End Cases
  114. End Select
  115.  
  116. End Sub
  117.  
  118. Private Sub Workbook_SheetSelectionChange _
  119. ( _
  120. ByVal TheWorksheet As Object, _
  121. ByVal Target As Range _
  122. )
  123.  
  124. RevertSelectionIfRequired
  125.  
  126. End Sub
Add Comment
Please, Sign In to add comment