简体   繁体   中英

VBA for excel Question : How to make VBA search for multiple texts in ranges?

Hello i want to make a form that have 2 or 3 text boxes that i can enter text values in it and vba search for this values if they are matched in 1 cell like if i enter "hello" in the first text box then i entered "world" in the other text box it search the hall collumn for a range that have "hello world" in it then copy paste it to another sheet and there are maybe multiple cells that have the same values till now i was able to do that for one value only , then i tried altering it to match wat i need but i couldnt here is wat i am at :

Private Sub CommandButton1_Click()

Dim rng As range

For Each rng In Worksheets("sheet1").range("a:a")
If rng.Value = TextBox1 Then
rng.EntireRow.Select
Selection.Copy Destination:=Worksheets("Sheet2").range("A" & Worksheets("Sheet2").range("A65536").End(xlUp).Row + 1)
End If
Next rng
End Sub

and actualy this one only works on finding the cell that only have the word i am searching for per example if i am searchin for "Java" and there is a cell that have .net and java it doesnt consider it in the copy process and i want it to find any cell with the search values i gave it tyvm

I am not sure whether I totally got your question. But if you want to know whether one value is in a cell and another one. You need to use InStr.

The IF Statement above would look something like this (haven't tested it but should work)

If InStr(rng.Value, TextBox1) > 0 and InStr(rng.Value, TextBox2) > 0 then

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM