MitchiBoy3
New Member
- Joined
- Feb 24, 2020
- Messages
- 4
- Office Version
- 2019
- Platform
- Windows
Hi everyone!
I have this VBA form that have the functions of a search box. When I type a word in the search box , click (not double click) the word in the list box to highlight and trying to erase the word in the search box to change my search word, run time error will pop up. When I debug the error, it points to the list box code (bolded text). I get this code from a website with free use of the codes, credits to Mr. Gergely Gyetvai. I tried to contact the author, until now i have no reply received. Can some help me on this? thanks so much.
THE WHOLE CODE BELOW
------------------------------------------------------------------------
Option Explicit
Private Sub UserForm_Initialize()
'Load list during initalization
Call loadList
Me.tbox_srch_ID.SetFocus
End Sub
'------------------------------------------------
Private Sub cmd_add_Click()
Call addItemByClick
End Sub
Private Sub lbox_ID_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call addItemByClick
End Sub
Private Sub lbox_Word_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call addItemByClick
End Sub
Private Sub cmb_cancel_Click()
Unload Me
End Sub
'------------------------------------------------
Private Sub lbox_ID_Change()
Me.lbox_Word.ListIndex = Me.lbox_ID.ListIndex
Me.lbox_Word.TopIndex = Me.lbox_ID.TopIndex
End Sub
Private Sub lbox_Word_Change()
Me.lbox_ID.ListIndex = Me.lbox_Word.ListIndex
Me.lbox_ID.TopIndex = Me.lbox_Word.TopIndex
End Sub
Private Sub tbox_srch_ID_Keyup(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Me.tbox_srch_Word.Value = ""
Call loadList
End Sub
Private Sub tbox_srch_Word_Keyup(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Me.tbox_srch_ID.Value = ""
Call loadList
End Sub
' ----------------------------------------------------------------
Sub loadList()
Dim baseArray() As Variant
Dim resultArray() As Variant
Dim IDArray() As Variant
Dim wordArray() As Variant
Dim counter As Long, i As Long
On Error Resume Next
Me.lbox_ID.Clear
Me.lbox_Word.Clear
baseArray = WordList.Range("tbl_WordList")
counter = 0
For i = LBound(baseArray) To UBound(baseArray)
If ((InStr(1, baseArray(i, 1), Me.tbox_srch_ID.Value, vbTextCompare) > 0 And Me.tbox_srch_Word.Value = "") Or _
(InStr(1, baseArray(i, 2), Me.tbox_srch_Word.Value, vbTextCompare) > 0 And tbox_srch_ID.Value = "")) Then
counter = counter + 1
ReDim Preserve resultArray(1 To 2, 1 To counter)
resultArray(1, counter) = baseArray(i, 1)
resultArray(2, counter) = baseArray(i, 2)
End If
Next i
If counter > 0 Then
ReDim IDArray(1 To UBound(resultArray, 2), 1 To 1)
ReDim wordArray(1 To UBound(resultArray, 2), 1 To 1)
For i = LBound(resultArray, 2) To UBound(resultArray, 2)
IDArray(i, 1) = resultArray(1, i)
wordArray(i, 1) = resultArray(2, i)
Next i
Me.lbox_ID.List = IDArray
Me.lbox_Word.List = wordArray
End If
On Error GoTo 0
End Sub
' ----------------------------------------------------------------
Sub addItemByClick()
Call addWord
Me.Hide
End Sub
' ----------------------------------------------------------------
Sub addWord()
Dim selectedValue As String, selectedIndex As Long
Dim cell As Range
selectedValue = ""
Set cell = ActiveCell
If Me.lbox_ID.ListIndex > -1 Then
selectedIndex = Me.lbox_ID.ListIndex
selectedValue = Me.lbox_ID.List(selectedIndex)
cell.Value = "'" & selectedValue
End If
End Sub
--------------------------------------------------------------------
I get this whole code from a open source website and I tried to
I have this VBA form that have the functions of a search box. When I type a word in the search box , click (not double click) the word in the list box to highlight and trying to erase the word in the search box to change my search word, run time error will pop up. When I debug the error, it points to the list box code (bolded text). I get this code from a website with free use of the codes, credits to Mr. Gergely Gyetvai. I tried to contact the author, until now i have no reply received. Can some help me on this? thanks so much.
THE WHOLE CODE BELOW
------------------------------------------------------------------------
Option Explicit
Private Sub UserForm_Initialize()
'Load list during initalization
Call loadList
Me.tbox_srch_ID.SetFocus
End Sub
'------------------------------------------------
Private Sub cmd_add_Click()
Call addItemByClick
End Sub
Private Sub lbox_ID_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call addItemByClick
End Sub
Private Sub lbox_Word_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call addItemByClick
End Sub
Private Sub cmb_cancel_Click()
Unload Me
End Sub
'------------------------------------------------
Private Sub lbox_ID_Change()
Me.lbox_Word.ListIndex = Me.lbox_ID.ListIndex
Me.lbox_Word.TopIndex = Me.lbox_ID.TopIndex
End Sub
Private Sub lbox_Word_Change()
Me.lbox_ID.ListIndex = Me.lbox_Word.ListIndex
Me.lbox_ID.TopIndex = Me.lbox_Word.TopIndex
End Sub
Private Sub tbox_srch_ID_Keyup(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Me.tbox_srch_Word.Value = ""
Call loadList
End Sub
Private Sub tbox_srch_Word_Keyup(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Me.tbox_srch_ID.Value = ""
Call loadList
End Sub
' ----------------------------------------------------------------
Sub loadList()
Dim baseArray() As Variant
Dim resultArray() As Variant
Dim IDArray() As Variant
Dim wordArray() As Variant
Dim counter As Long, i As Long
On Error Resume Next
Me.lbox_ID.Clear
Me.lbox_Word.Clear
baseArray = WordList.Range("tbl_WordList")
counter = 0
For i = LBound(baseArray) To UBound(baseArray)
If ((InStr(1, baseArray(i, 1), Me.tbox_srch_ID.Value, vbTextCompare) > 0 And Me.tbox_srch_Word.Value = "") Or _
(InStr(1, baseArray(i, 2), Me.tbox_srch_Word.Value, vbTextCompare) > 0 And tbox_srch_ID.Value = "")) Then
counter = counter + 1
ReDim Preserve resultArray(1 To 2, 1 To counter)
resultArray(1, counter) = baseArray(i, 1)
resultArray(2, counter) = baseArray(i, 2)
End If
Next i
If counter > 0 Then
ReDim IDArray(1 To UBound(resultArray, 2), 1 To 1)
ReDim wordArray(1 To UBound(resultArray, 2), 1 To 1)
For i = LBound(resultArray, 2) To UBound(resultArray, 2)
IDArray(i, 1) = resultArray(1, i)
wordArray(i, 1) = resultArray(2, i)
Next i
Me.lbox_ID.List = IDArray
Me.lbox_Word.List = wordArray
End If
On Error GoTo 0
End Sub
' ----------------------------------------------------------------
Sub addItemByClick()
Call addWord
Me.Hide
End Sub
' ----------------------------------------------------------------
Sub addWord()
Dim selectedValue As String, selectedIndex As Long
Dim cell As Range
selectedValue = ""
Set cell = ActiveCell
If Me.lbox_ID.ListIndex > -1 Then
selectedIndex = Me.lbox_ID.ListIndex
selectedValue = Me.lbox_ID.List(selectedIndex)
cell.Value = "'" & selectedValue
End If
End Sub
--------------------------------------------------------------------
I get this whole code from a open source website and I tried to