1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| ' Module: User form to select an employees (c) P.Seuret v2015-03-06a
'
' Can't use RowSource of ListBox to filter data because it is strongly related
' to the data range. We thus choose store source data into an array (gDataSource)
' and load only items containing the filter value
'
' Global objects
Private gLinkedCell As Range
Private gDataSource() As Variant
' Set data source of the selector
Public Sub OpenForm(linkedCell As Range, dataSource As Range, linkedColumn As Integer, colWidths As Variant)
Dim hostCell As Range
Dim hostSheet As Worksheet
Dim widths As String
Dim totalW As Double
Dim iWidth As Integer
Dim colCount As Integer
Dim currentValue As String
Dim countItems As Integer
Dim dataRange As Range
Dim iRow As Integer
Dim iCol As Integer
Dim rowCount As Integer
On Error GoTo OpenForm_Error
' Check parameters
If ((linkedCell Is Nothing) Or (dataSource Is Nothing)) Then
GoTo OpenForm_End
End If
' Init variables
Set hostCell = linkedCell.Cells(1, 1)
Set hostSheet = linkedCell.parent
' Get values of dataSource range in gDataSource array
rowCount = dataSource.Rows.count
colCount = UBound(colWidths) - LBound(colWidths) + 1
colCount = IIf(colCount < dataSource.Columns.count, colCount, dataSource.Columns.count)
gDataSource = Range(dataSource.Cells(1, 1), dataSource.Cells(rowCount, colCount))
' Prepare width of columns
totalW = 0
widths = vbNullString
For iWidth = LBound(colWidths) To UBound(colWidths)
aWidth = colWidths(iWidth)
totalW = totalW + aWidth
widths = widths & IIf(widths = "", "", ";") & aWidth & "cm"
Next iWidth
' Set parameters of the listbox
With lstEmployees
'+PSE:2015-03-06:Replaced by array to allow filtering
'.RowSource = dataSource
'-PSE:2015-03-06
If (colCount > 0) Then
.ColumnCount = colCount
.columnWidths = VBA.Replace(widths, ".", ",")
Else
.ColumnCount = 1
.columnWidths = vbNullString
End If
.BoundColumn = linkedColumn
End With
' Load data into listbox with no filtering
txtFilter.text = vbNullString
LoadAndFilterDataItems
' Keep linked cell for later assignation of result value
Set gLinkedCell = hostCell
' Preselect current value
countItems = lstEmployees.ListCount
currentValue = hostCell.Value
lstEmployees.ListIndex = -1
If (currentValue <> "") Then
For iItem = 0 To (countItems - 1)
If (lstEmployees.List(iItem) = currentValue) Then
lstEmployees.ListIndex = iItem
Exit For
End If
Next iItem
End If
' Make form visible
Me.Show
' Done
GoTo OpenForm_End
OpenForm_Error:
Debug.Print "UserFormSelectEmployee::OpenForm: Error"
OpenForm_End:
' Free memory
Set hostCell = Nothing
Set hostSheet = Nothing
Set dataRange = Nothing
End Sub
' Load data in listbox
Private Sub LoadAndFilterDataItems(Optional filter As String = vbNullString, _
Optional fullSearch As Boolean = False)
Dim iRow As Integer
Dim rowCount As Integer
Dim iCol As Integer
Dim iColFilter As Integer
Dim colCount As Integer
Dim filterLen As Integer
Dim filterValue As String
Dim doMatch As Boolean
Dim isScreenUpdating As Boolean
On Error GoTo LoadAndFilterDataItems_Error
' Lock display
isScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
' Init variables
rowCount = 1 + UBound(gDataSource, 1) - LBound(gDataSource, 1)
colCount = 1 + UBound(gDataSource, 2) - LBound(gDataSource, 2)
If ((rowCount = 0) Or (colCount = 0)) Then
Exit Sub
End If
' Clear old data
lstEmployees.Clear
' Fill listbox
If (filter = "") Then
' No filter... Load quickly
With lstEmployees
For iRow = 1 To rowCount
.AddItem gDataSource(iRow, 1)
If (colCount > 1) Then
For iCol = 2 To colCount
.List(iRow - 1, iCol - 1) = gDataSource(iRow, iCol)
Next iCol
End If
Next iRow
End With
Else
'Apply filter (case insensitive)
filterValue = VBA.LCase(txtFilter.text)
filterLen = VBA.Len(filterValue)
With lstEmployees
For iRow = 1 To rowCount
' Does item match filter ?
For iColFilter = 1 To colCount
If (fullSearch) Then
doMatch = (VBA.InStr(1, VBA.LCase(gDataSource(iRow, iColFilter)), filterValue) > 0)
Else
doMatch = (VBA.LCase(VBA.Left(gDataSource(iRow, iColFilter), filterLen)) = filterValue)
End If
If (doMatch) Then
' Yes! Add as new item
.AddItem gDataSource(iRow, 1)
If (colCount > 1) Then
For iCol = 2 To colCount
.List(.ListCount - 1, iCol - 1) = gDataSource(iRow, iCol)
Next iCol
End If
' Stop filter search for this row
Exit For
End If
Next iColFilter
Next iRow
End With
End If
' Finished
GoTo LoadAndFilterDataItems_End
LoadAndFilterDataItems_Error:
Debug.Print "UserFormSelectEmployee::LoadAndFilterDataItems: Error"
LoadAndFilterDataItems_End:
' Restore display
Application.ScreenUpdating = isScreenUpdating
' Free memory
End Sub
' Reset and close form
Private Sub CloseForm()
On Error Resume Next
' Reset parameters
Erase gDataSource
Set gLinkedCell = Nothing
lstEmployees.RowSource = vbNullString
txtFilter.text = vbNullString
' Hide form
Me.Hide
End Sub
Private Sub chkFullSearch_Click()
' Same as a change in filter
txtFilter_Change
End Sub
' User cancel his choice
Private Sub cmdCancel_Click()
CloseForm
End Sub
' User apply his choice
Private Sub cmdOk_Click()
Dim Result As String
If (Not gLinkedCell Is Nothing) Then
If (lstEmployees.ListIndex >= 0) Then
gLinkedCell.Value = lstEmployees.List(lstEmployees.ListIndex)
Else
gLinkedCell.Value = vbNullString
End If
End If
CloseForm
End Sub
' User make his choice
Private Sub lstEmployees_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
cmdOk_Click
End Sub
' Apply filter to the list
Private Sub txtFilter_Change()
Dim dataSource As String
Dim filterValue As String
Dim isFullSearch As Boolean
Dim isScreenUpdating As Boolean
On Error GoTo txtFilter_Change_Error
' Lock display
isScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
' Get parameters
isFullSearch = chkFullSearch.Value
filterValue = txtFilter.text
' Filter data
LoadAndFilterDataItems filter:=filterValue, fullSearch:=isFullSearch
' Finished
GoTo txtFilter_Change_End
txtFilter_Change_Error:
Debug.Print "UserFormSelectEmployee::txtFilter_Change: Error"
txtFilter_Change_End:
' Restore display
Application.ScreenUpdating = isScreenUpdating
' Free memory
End Sub
Private Sub UserForm_Activate()
' Give focus to the filter text
txtFilter.SetFocus
End Sub |
Partager