简体   繁体   English

VBA for excel问题:如何使VBA搜索范围内的多个文本?

[英]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 : 您好,我想制作一个具有2个或3个文本框的表单,可以在其中输入文本值,如果它们在1个单元格中匹配,则vba会搜索该值,例如,如果我在第一个文本框中输入“ hello”,然后输入在另一个文本框中,“世界”会在大厅列中搜索一个包含“世界你好”的范围,然后将其复制粘贴到另一张纸上,也许有多个具有相同值的单元格,直到现在我都能做到这一点仅提供一个值,然后我尝试将其更改为与我需要的wat匹配,但我无法在这里输入wat:

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 实际上,这仅适用于查找仅包含我正在搜索的单词的单元格(例如,如果我正在搜索“ Java”),并且有一个具有.net和java的单元格,则在复制过程中不考虑它,而我希望它找到我给它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. 您需要使用InStr。

The IF Statement above would look something like this (haven't tested it but should work) 上面的IF语句看起来像这样(尚未对其进行测试,但应该可以工作)

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

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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