Function 在Excel VBA中创建函数,以测试单元格是否包含特定字符

Function 在Excel VBA中创建函数,以测试单元格是否包含特定字符,function,vba,excel,Function,Vba,Excel,我希望在Excel/VBA中创建一个函数,该函数将查看一系列单元格,如果其中任何单元格包含某个字符(星号*),则返回TRUE值。将有一些空白单元格,一些单元格将包含文本,一些单元格可能包含文本和星号。有人能帮忙吗?非常感谢将以下代码复制并粘贴到新模块中 Function ContainsStar(r As Range) as Boolean Dim i As Long Dim cell As Range Dim contains As Boolean For Ea

我希望在Excel/VBA中创建一个函数,该函数将查看一系列单元格,如果其中任何单元格包含某个字符(星号*),则返回TRUE值。将有一些空白单元格,一些单元格将包含文本,一些单元格可能包含文本和星号。有人能帮忙吗?非常感谢

将以下代码复制并粘贴到新模块中

Function ContainsStar(r As Range) as Boolean
    Dim i As Long
    Dim cell As Range
    Dim contains As Boolean
    For Each cell In r.Cells
        For i = 1 To Len(cell)
            If Right(Left(cell, i), 1) = Chr(42) Then
                contains = True
                GoTo ExitFn
            End If
        Next i
    Next
    Exit Function
ExitFn:
    ContainsStar = contains
    Exit Function
End Function
然后像这样在电子表格中使用它

注:

D1=
=ConstainsStar(A1:A3)

H1=
=ConstainsStar(E1:E3)


将以下代码复制并粘贴到新模块中

Function ContainsStar(r As Range) as Boolean
    Dim i As Long
    Dim cell As Range
    Dim contains As Boolean
    For Each cell In r.Cells
        For i = 1 To Len(cell)
            If Right(Left(cell, i), 1) = Chr(42) Then
                contains = True
                GoTo ExitFn
            End If
        Next i
    Next
    Exit Function
ExitFn:
    ContainsStar = contains
    Exit Function
End Function
然后像这样在电子表格中使用它

注:

D1=
=ConstainsStar(A1:A3)

H1=
=ConstainsStar(E1:E3)


将以下代码复制并粘贴到新模块中

Function ContainsStar(r As Range) as Boolean
    Dim i As Long
    Dim cell As Range
    Dim contains As Boolean
    For Each cell In r.Cells
        For i = 1 To Len(cell)
            If Right(Left(cell, i), 1) = Chr(42) Then
                contains = True
                GoTo ExitFn
            End If
        Next i
    Next
    Exit Function
ExitFn:
    ContainsStar = contains
    Exit Function
End Function
然后像这样在电子表格中使用它

注:

D1=
=ConstainsStar(A1:A3)

H1=
=ConstainsStar(E1:E3)


将以下代码复制并粘贴到新模块中

Function ContainsStar(r As Range) as Boolean
    Dim i As Long
    Dim cell As Range
    Dim contains As Boolean
    For Each cell In r.Cells
        For i = 1 To Len(cell)
            If Right(Left(cell, i), 1) = Chr(42) Then
                contains = True
                GoTo ExitFn
            End If
        Next i
    Next
    Exit Function
ExitFn:
    ContainsStar = contains
    Exit Function
End Function
然后像这样在电子表格中使用它

注:

D1=
=ConstainsStar(A1:A3)

H1=
=ConstainsStar(E1:E3)


您也可以使用instr功能

function containstar(byval r as range) as boolean
dim cel as range 'be careful about variable names that are already existing code (cell)
for each cel in r.cells   
'or use a loop like: for i=1 to r.cells.count , and use r.cells(i), if you prefer.
  if instr(1,cel.value,"*")>0 then
     containstar=true
     exit sub   'you can also exit for
  end if
next cel
containstar=false
end sub
并调用该函数:

a=containstar("a1:b8")

只有当你不能做其他事情时才使用goto,通常你不需要它。

你也可以使用instr功能

function containstar(byval r as range) as boolean
dim cel as range 'be careful about variable names that are already existing code (cell)
for each cel in r.cells   
'or use a loop like: for i=1 to r.cells.count , and use r.cells(i), if you prefer.
  if instr(1,cel.value,"*")>0 then
     containstar=true
     exit sub   'you can also exit for
  end if
next cel
containstar=false
end sub
并调用该函数:

a=containstar("a1:b8")

只有当你不能做其他事情时才使用goto,通常你不需要它。

你也可以使用instr功能

function containstar(byval r as range) as boolean
dim cel as range 'be careful about variable names that are already existing code (cell)
for each cel in r.cells   
'or use a loop like: for i=1 to r.cells.count , and use r.cells(i), if you prefer.
  if instr(1,cel.value,"*")>0 then
     containstar=true
     exit sub   'you can also exit for
  end if
next cel
containstar=false
end sub
并调用该函数:

a=containstar("a1:b8")

只有当你不能做其他事情时才使用goto,通常你不需要它。

你也可以使用instr功能

function containstar(byval r as range) as boolean
dim cel as range 'be careful about variable names that are already existing code (cell)
for each cel in r.cells   
'or use a loop like: for i=1 to r.cells.count , and use r.cells(i), if you prefer.
  if instr(1,cel.value,"*")>0 then
     containstar=true
     exit sub   'you can also exit for
  end if
next cel
containstar=false
end sub
并调用该函数:

a=containstar("a1:b8")

只有当你不能做其他事情时才使用goto,通常你不需要它。

+1使用
~*
+1使用
~*
+1使用
~*
+1使用
~*
谢谢,我现在让它工作起来了,我现在让它工作起来了,我现在让它工作起来了,我现在让它工作起来了