modification Code vba

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Welcome . How can I search in sheet2 only because the code searches all sheets

VBA Code:
Private Sub TextBox1_Change()
If TextBox1.Value = "" Then ListBox2.Clear: Exit Sub
Dim x As Worksheet
         ListBox2.Clear
    k = 0
For Each x In ThisWorkbook.Worksheets
        ss = x.Cells(Rows.Count, 3).End(xlUp).Row
        For Each c In x.Range("c2:c" & ss)
            b = InStr(c, TextBox1)
            If b > 0 Then
                ListBox2.AddItem
                ListBox2.List(k, 0) = x.Cells(c.Row, 3).Value
                

                k = k + 1
            End If
        Next c

Next x
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Get rid of your loop:
VBA Code:
For Each x In ThisWorkbook.Worksheets
and corresponding:
VBA Code:
Next x
line.

And then add this line at the top:
VBA Code:
Set x = Sheets("Sheet2")
 
Upvote 0
I simplified your code a bit:

VBA Code:
Private Sub TextBox1_Change()
  Dim c As Range
  
  ListBox2.Clear
  If TextBox1.Value = "" Then Exit Sub
  With Sheets("Sheet2")
    For Each c In .Range("C2", .Cells(Rows.Count, 3).End(xlUp))
      If InStr(c.Value, TextBox1) > 0 Then
        ListBox2.AddItem c.Value
      End If
    Next c
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

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