Excel VBA如果cell.Value = ...则 [英] Excel VBA If cell.Value =... then

查看:807
本文介绍了Excel VBA如果cell.Value = ...则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码,效果很好:

I have this code which works well:

Sub colortest()
Dim cell As Range
For Each cell In Range("Range1")
If cell.Value = "Word1" Then
cell.Interior.Color = XlRgbColor.rgbLightGreen
ElseIf cell.Value = "Word2" Then
cell.Interior.Color = XlRgbColor.rgbOrange
ElseIf cell.Value = "Word3" Then
cell.Interior.Color = XlRgbColor.rgbRed
End If
Next cell
End Sub

我的问题是,仅当单元格仅包含该文本时,值才起作用.但是我的单元格通常是这样的:"10254、15/15, Word1 另一个单词" 我只需要 Word1 . 谢谢

My problem is the values work only if the cell contains only that text. But my cells are usually like this: "Something, 10254, 15/15, Word1 Another Word" I only need the Word1. Thank you,

推荐答案

您可以通过使用

If InStr(cell.Value, "Word1") > 0 Then

如果在字符串中找到Word1,则InStr()函数将返回字符串中Word1的第一个字符的位置.

If Word1 is found in the string the InStr() function will return the location of the first character of Word1 in the string.

这篇关于Excel VBA如果cell.Value = ...则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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