开发者

excel vba - custom formula for Contains()

开发者 https://www.devze.com 2023-04-01 23:50 出处:网络
If i had a column like: ColumnA ------- NUMBER VARCHAR(50 BYTE) TIMESTAMP I\'m looking for a vba function that would work like so:

If i had a column like:

ColumnA
-------
NUMBER
VARCHAR(50 BYTE)
TIMESTAMP

I'm looking for a vba function that would work like so:

contains("string to search for", cell to search it in) ie. =contains("VARCHAR",A2) and if the cell does contain it (anywhere in the cell - doesn't have to start or end the cell) then populate the cell (that uses the function) with "T" else "F".

so in my example:

ColumnB
-------
F
T
F

Also could I then use this function inside other VBA code like so:

If Len(A2) > 10 and contains("CHAR",A2) Then
 //do stuff
End If

If they have to be separate functions (to use in cel开发者_高级运维ls returning "T"/"F" and to use in other VBA code), please provide both.


You want VBA's InStr

For example in a module;

Public Function Contains(Text As String, Cell As Range) As String
   If InStr(1, Cell.Value, Text, vbTextCompare) Then '//case insensitive
       Contains = "T"
   Else
       Contains = "F"
   End If
End Function

Enables =Contains("blah", A1) in a worksheet

0

精彩评论

暂无评论...
验证码 换一张
取 消