Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel vba:如果单元格包含整个单词,则_Excel_Vba - Fatal编程技术网

Excel vba:如果单元格包含整个单词,则

Excel vba:如果单元格包含整个单词,则,excel,vba,Excel,Vba,您好,我正在尝试编写一段代码,以确定字符串单元格(2,5)=“此情况不好”和单元格(3,5)=“我想坐下”是否包含单词“坐下” 理想情况下,单元格(2,5)将返回0,单元格(3,5)将返回1 现在我有 for i = 2 to 3 if LCase(cells(i,5)) like "*sit*" then cells(2,5) = 1 end if next i 问题是单元格(2,5)包含单词“情境”,因此“sit”,在这种情况下,我的代码将返回1 我也试过了 if instr(

您好,我正在尝试编写一段代码,以确定字符串单元格(2,5)=“此情况不好”单元格(3,5)=“我想坐下”是否包含单词“坐下”

理想情况下,单元格(2,5)将返回0,单元格(3,5)将返回1

现在我有

for i = 2 to 3
  if LCase(cells(i,5)) like "*sit*" then
  cells(2,5) = 1
  end if
next i
问题是单元格(2,5)包含单词“情境”,因此“sit”,在这种情况下,我的代码将返回1

我也试过了

if instr(1,cells(i,5),"sit") <> 0 then
cells(i,5) = 1
如果instr(1,单元格(i,5),“sit”)为0,则
单元(i,5)=1
它给出了同样不准确的结果


我想知道,当且仅当单元格包含整个单词时,我如何才能使某些内容返回到1?

您可以使用正则表达式,因为您希望找到的单词不仅仅是子字符串。这将当前在相邻的右侧列中输出结果

Option Explicit
Public Sub IsWordFound()
    Dim arr(), i As Long
    With Worksheets("Sheet1")
        arr = .Range("E2:E3").Value
        ReDim Preserve arr(1 To UBound(arr, 1), 1 To UBound(arr, 1))
        For i = LBound(arr, 1) To UBound(arr, 1)
            arr(i, 2) = IsFound(arr(i, 1), "sit")
        Next i
       .Range("E2").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    End With
End Sub

Public Function IsFound(ByVal inputString As String, ByVal word As String) As Byte
    Dim  re As Object
    Set re = CreateObject("VBScript.RegExp")
    With re
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = "\b" & word & "\b"
        If .test(inputString) Then
           IsFound = 1
        Else
           IsFound = 0
        End If
    End With
End Function
考虑:

Public Function CheckForSit(inpt As String) As Long
    Dim examin As String, patrn As String

    examin = " " & LCase(inpt) & " "
    patrn = " sit "

    If InStr(examin, patrn) > 0 Then
        CheckForSit = 1
    Else
        CheckForSit = 0
    End If
End Function


这假设没有标点符号,如sit.sit?

检查您的单词是否用空格或标点符号填充?