Find Text from Userform TextBox

Rex2024

New Member
Joined
Nov 17, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I have a textbox on a userform which a macro populates a text string from another data source.

I want the user to be able to search in that textbox for a phrase of their choice. I'm fine with either centering the scroll on the text or bolding/underlining the specific phrases, but am struggling to do it.

This code supposedly works in worksheets, but how can I modify it to work in a userform textbox?

VBA Code:
   Dim shp As Shape
    Dim sFind As String
    Dim sTemp As String
    Dim iPos As Integer
    Dim Response

    sFind = InputBox("Search for?")
    If Trim(sFind) = "" Then
        MsgBox "Nothing entered"
        Exit Sub
    End If
    sFind = LCase(sFind)
    For Each shp In ActiveSheet.Shapes
        sTemp = LCase(shp.TextFrame2.TextRange.Characters.Text)
        iPos = InStr(sTemp, sFind)
        If iPos > 0 Then
            With shp.TextFrame2.TextRange.Characters(Start:=iPos, _
              Length:=Len(sFind)).Font
                .UnderlineStyle = msoUnderlineHeavyLine
                .Bold = True
            End With
        End If
    Next
    MsgBox "Finished"
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I've tried this code as well and it doesn't seem to be working.

VBA Code:
Private Sub SearchButton_Click()

    Dim searchText As String
    searchText = Me.Search.Value
   
    If Len(searchText) = 0 Then
        MsgBox "Enter a search term, and try again!", vbExclamation
        Exit Sub
    End If
   
    Dim itm As ListSubItem
    Dim i As Long
    Dim j As Long
    With Me.ListView1
        For i = 1 To .ListItems.Count
            With .ListItems(i)
                For j = 1 To .ListSubItems.Count
                    If LCase(.ListSubItems(j)) Like "*" & LCase(searchText) & "*" Then
                        Debug.Print .Text, .ListSubItems(j)
                        Exit For
                    End If
                Next j
            End With
        Next i
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top