Excel 如何使用;包括「;VBA中的函数?

Excel 如何使用;包括「;VBA中的函数?,excel,vba,Excel,Vba,这是我的Excel表格,有3个输入列。最后一列是我需要的结果的输出列 以下是我的示例宏: 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 = "Jan

这是我的Excel表格,有3个输入列。最后一列是我需要的结果的输出列

以下是我的示例宏:

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

我的工作表包含数千行,其中包括作为子字符串的“一月”以及单元格中的文本。我希望通过检查字符串“x”或字符串“y”是否包含字符串“janur”,来简化宏,而不是编写多个检查,如“if”x=janur.winter“。有什么方法可以改变宏来实现这一点吗?

我想到了三种方法:

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$()
来强制文本小写,并防止出现任何大写问题-在比较字符串时总是值得考虑的问题。

想到三种方法:

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$()
强制文本小写,并防止出现任何大写问题-在比较字符串时总是值得考虑的问题。

您可以使用InStr函数。示例用法:

If (InStr(x,"January") > 0 Or InStr(y,"January")>0) And (z = "Jan") Then
 ...
如果未找到子字符串,则返回0,否则返回子字符串的位置

更多信息


还要小心上下壳体。“一月”与“一月”不匹配。使用LCase和UCase函数强制上下套管,就像Macro Man在回答中所做的那样。

您可以使用InStr函数。示例用法:

If (InStr(x,"January") > 0 Or InStr(y,"January")>0) And (z = "Jan") Then
 ...
如果未找到子字符串,则返回0,否则返回子字符串的位置

更多信息

还要小心上下壳体。“一月”与“一月”不匹配。使用LCase和UCase函数强制上下套管,就像Macro Man在回答中所做的那样