如何在Excelmacros中使用“包含”function

我是新的macros观的macros。 这是我的excel表,有3个input列,最后一列是输出列和我需要的结果。

Excelinput和输出数据

这里是我有的示例macros:

Function month(x As string, y As string, z As String) As String If (((x = "January.Winter") Or (y = "January.Winter")) And (z = "jan")) Then month= "true" Else If (((x = "January.2016") Or (y = "January.2016")) And (z = "jan")) Then month= "true" Else If (((x = "January.today") Or (y = "January.today")) And (z = "jan")) Then month= "true" Else month= False End If End Function 

我的excel包含成千上万的行,其中包括“1月”作为一个子string作为单元格中的文本。 而不是像“if”x = January.winter“”写多个检查,我想通过检查string“x”或string“y”是否包含string“1月”来简化macros。 有没有办法可以改变macros来做到这一点?

谢谢。

想到三种方法:

 If LCase$(x) Like "january*" Or LCase$(y) Like "january*" Then ... If InStr(LCase$(x), "january") Or InStr(LCase$(x), "january") Then ... If LCase$(Left$(x, 7)) = "january" Or LCase$(Left$(y, 7)) = "january" Then ... 

这真的只取决于你想得到多么有创意。


请注意,我使用LCase$()强制文本小写,并防止capitilisation的任何问题 – 比较string时总是值得思考的东西。

您可以使用InStrfunction。 使用示例:

 If (InStr(x,"January") > 0 Or InStr(y,"January")>0) And (z = "Jan") Then ... 

如果你的子string没有find,它返回0,否则返回你的子string的位置。

更多信息在这里

还要小心上下壳。 “一月”不符合“一月”。 就像macros观人在他的回答中所做的那样,使用LCase和UCase函数来强制上下套pipe。