在Excel VBA中创建一个函数来测试一个单元格是否包含某个特定字符 [英] Create a function in Excel VBA to test if a cell contains a certain character

查看:2386
本文介绍了在Excel VBA中创建一个函数来测试一个单元格是否包含某个特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

 函数ContainsStar(r As Range)as Boolean b $ b Dim i As Long 
Dim cell As Range
Dim contains As Boolean
For Each如果右(左(cell,i),1)= Chr(42),那么
包含=真
GoTo ExitFn
End If
Next i
Next
Exit Function
ExitFn:
ContainsStar = contains
Exit Function
End功能

然后像这样在电子表格中使用它



注意:
$ b

D1 = = ConstainsStar(A1:A3)



H1 = = ConstainsStar(E1:E3)




I'm looking to create a function in Excel/VBA that will look at a range of cells and return a value of TRUE if any of the cells contains a certain character (an asterisk *). There will be some blank cells, some cells will contain text and some may contain text and an asterisk. Can anyone help? Many thanks

解决方案

Copy and paste the below code into a new module

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

then use it in the spreadsheet like this

Note:

D1 = =ConstainsStar(A1:A3)

H1 = =ConstainsStar(E1:E3)

这篇关于在Excel VBA中创建一个函数来测试一个单元格是否包含某个特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆