VBA中for循环内部的If语句

VBA中for循环内部的If语句,vba,excel,Vba,Excel,我试图搜索一个列(在我的例子中是第3列),看看它是否有传递到函数Extract中的字符串。当If语句命中时,它会从同一行的不同列(在我的例子中是第6列)复制文本,并退出For循环。函数中的For循环用于扫描第3列中的所有行。为了检查是否匹配,我使用了VBA中可用的工作表函数 Function Extract(x As String, Y As Integer) As String Dim i As Integer For i = 2 To Y If Appli

我试图搜索一个列(在我的例子中是第3列),看看它是否有传递到函数Extract中的字符串。当If语句命中时,它会从同一行的不同列(在我的例子中是第6列)复制文本,并退出
For
循环。函数中的
For
循环用于扫描第3列中的所有行。为了检查是否匹配,我使用了VBA中可用的工作表函数

Function Extract(x As String, Y As Integer) As String

    Dim i As Integer

    For i = 2 To Y
        If Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Find(x, Cells(i, 3))) = True Then
            Extract = Cells(i, 6)
            Exit For   
        End If
    Next i

End Function

我试过运行我编写的这段代码,但似乎不起作用。

编辑了添加“部分匹配”功能的代码

  • 部分匹配

    使用
    Range
    对象的
    Find()
    方法:

    Function Extract(x As String, Y As Integer) As String    
        Dim f As Range
    
        Set f = Range(Cells(2, 3), Cells(Y, 3)).Find(what:=x, lookAt:=xlPart, LookIn:=xlValues) '<--| "xlPart" value of "lookAt" argument makes the "partial" match functionality
        If Not f Is Nothing Then Extract = f.Offset(,3).Text    
    End Function
    
    Function Extract(x As String, Y As Integer) As String
        Dim f As Variant
    
        f = Application.VLookup("*" & x & "*", Range(Cells(2, 3), Cells(Y, 6)), 4, False) '<--| the "False" argument and the asterisks ("*") makes the "partial" match functionality
        If Not IsError(f) Then Extract = f
    End Function
    
    Function Extract(x As String, Y As Integer) As String    
        Dim f As Range
    
        Set f = Range(Cells(2, 3), Cells(Y, 3)).Find(what:=x, lookAt:=xlWhole, LookIn:=xlValues) '<--| "xlWhole" value of "lookAt" argument makes the "exact" match functionality
        If Not f Is Nothing Then Extract = f.Offset(,3).Text    
    End Function
    
    Function Extract(x As String, Y As Integer) As String
        Dim f As Variant
    
        f = Application.VLookup(x, Range(Cells(2, 3), Cells(Y, 6)), 4, False)'<--| the "False" 4th argument makes the "exact" match functionality
        If Not IsError(f) Then Extract = f
    End Function
    
  • 精确匹配

    使用
    Range
    对象的
    Find()
    方法:

    Function Extract(x As String, Y As Integer) As String    
        Dim f As Range
    
        Set f = Range(Cells(2, 3), Cells(Y, 3)).Find(what:=x, lookAt:=xlPart, LookIn:=xlValues) '<--| "xlPart" value of "lookAt" argument makes the "partial" match functionality
        If Not f Is Nothing Then Extract = f.Offset(,3).Text    
    End Function
    
    Function Extract(x As String, Y As Integer) As String
        Dim f As Variant
    
        f = Application.VLookup("*" & x & "*", Range(Cells(2, 3), Cells(Y, 6)), 4, False) '<--| the "False" argument and the asterisks ("*") makes the "partial" match functionality
        If Not IsError(f) Then Extract = f
    End Function
    
    Function Extract(x As String, Y As Integer) As String    
        Dim f As Range
    
        Set f = Range(Cells(2, 3), Cells(Y, 3)).Find(what:=x, lookAt:=xlWhole, LookIn:=xlValues) '<--| "xlWhole" value of "lookAt" argument makes the "exact" match functionality
        If Not f Is Nothing Then Extract = f.Offset(,3).Text    
    End Function
    
    Function Extract(x As String, Y As Integer) As String
        Dim f As Variant
    
        f = Application.VLookup(x, Range(Cells(2, 3), Cells(Y, 6)), 4, False)'<--| the "False" 4th argument makes the "exact" match functionality
        If Not IsError(f) Then Extract = f
    End Function
    

您可以使用
Match
功能在第3列中查找x。如果第3列中有匹配项,您将得到该行号。如果没有,Extract将返回一个字符串,让您知道没有匹配项

注意:在我的测试中,
Extract
不会在我的Excel中运行,可能是保存的单词或其他东西,所以我修改为
Extract2

编辑1:修改代码以支持采购订单的查找部分字符串请求-在
x
前后添加了
*
通配符

Function Extract2(x As String, Y As Integer) As String

If Not IsError(Application.Match("*" & x & "*", Columns(3), 0)) Then ' successful match
    Extract2 = Cells(Application.Match("*" & x & "*", Columns(3), 0), 6)
Else  ' match was unable to find a result in column 3
    Extract2 = x & " not found in Column 3"
End If

End Function

在我的例子中,要搜索的列中的每个单元格都可以有多个字符串。将match_type设置为0仅搜索单元格中的确切字符串。有其他选择吗?
Match
也可以返回较大或较小的值(通过将第三个参数更改为1或-1),这是您的意思吗?您可以在此处阅读有关
匹配的内容:@jyotirmayapatra请参阅编辑的代码以支持在包含多个字符串的单元格中搜索您的字符串,如果这是您的意思,请告诉我。抱歉,响应太晚。您修改后的代码符合我的要求。非常感谢您的帮助!非常感谢你的帮助!