Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将两个单元格作为参数,并根据单元格的字符串值返回特定值_Excel_Vba - Fatal编程技术网

Excel 将两个单元格作为参数,并根据单元格的字符串值返回特定值

Excel 将两个单元格作为参数,并根据单元格的字符串值返回特定值,excel,vba,Excel,Vba,我需要创建一个VBA函数,该函数使用两个单元格的值作为参数,然后根据单元格的值返回一些内容。我是这么想的 Function returnNewString (cell1,cell2): if cell1 == 'somestring' && cell2 == "someotherstring" then return "something" else if cell1 == 'somestring2' && cell2 == "som

我需要创建一个VBA函数,该函数使用两个单元格的值作为参数,然后根据单元格的值返回一些内容。我是这么想的

   Function returnNewString (cell1,cell2):
    if cell1 == 'somestring' && cell2 == "someotherstring" then
    return "something"
    else if cell1 == 'somestring2' && cell2 == "someotherstring2" then
    return "something else"
   end function

因此,
returnNewString(A1,B1)
的函数调用应该返回与条件相关的“something”。

您可以使用嵌套的If/Case语句。非常简单,只需检查第一个字符串的值,然后继续检查第二个字符串的值

在您的特定情况下,我会选择Select Case语句,但最终会归结为偏好

Function returnNewString(sValue1 As String, sValue2 As String) As String

    Select Case sValue1
    Case "Some String"
        Select Case sValue2
        Case "SomeOtherString"
            returnNewString = "Something"
        Case "Some Other String 2"
            returnNewString = "Something else"
        End Select
    Case "Some String 2"
        Select Case sValue2
        Case "SomeOtherString"
            returnNewString = "Something"
        Case "Some Other String 2"
            returnNewString = "Something else"
        End Select
    End Select

End Function
在第一个Select案例中,检查第一个参数的值
sValue1
。匹配该值后,继续检查
sValue2
的值


您在示例中使用的语法不正确-VBA中没有
==
&
运算符,文字字符串值必须用双引号括起来,而不是单引号-单引号保留用于注释。

您可以使用嵌套的If/Case语句。非常简单,只需检查第一个字符串的值,然后继续检查第二个字符串的值

在您的特定情况下,我会选择Select Case语句,但最终会归结为偏好

Function returnNewString(sValue1 As String, sValue2 As String) As String

    Select Case sValue1
    Case "Some String"
        Select Case sValue2
        Case "SomeOtherString"
            returnNewString = "Something"
        Case "Some Other String 2"
            returnNewString = "Something else"
        End Select
    Case "Some String 2"
        Select Case sValue2
        Case "SomeOtherString"
            returnNewString = "Something"
        Case "Some Other String 2"
            returnNewString = "Something else"
        End Select
    End Select

End Function
在第一个Select案例中,检查第一个参数的值
sValue1
。匹配该值后,继续检查
sValue2
的值


您在示例中使用的语法不正确-VBA中没有
==
&
运算符,文字字符串值必须包含在双引号中,而不是单引号中-单引号保留用于注释。

为了完整性,编写的伪代码的实现将是(这将是非常敏感的案例)


为了实现完整性,您编写的伪代码的实现将是(区分大小写)


您确定正在使用VBA吗?您的代码示例与VBA完全不同。您确定正在使用VBA吗?您的代码示例与VBA完全不同。这将给OPs代码带来不同的结果(例如,如果cell1是“某个字符串”,cell2是“某个其他字符串2”)是和否-我试图展示他的选择。但是,如果他真的想让第二个select案例中的第二个陈述和另一个案例中的第一个陈述匹配1对1,他可以简单地删除第二个select案例中的第二个陈述。但显然不熟悉VBA的人至少应该在回答中向他们解释这一点,我没有做到这将给OPs代码带来不同的结果(例如,如果cell1是“某个字符串”,cell2是“某个其他字符串2”)是和否-我试图展示他的选择。但是,如果他真的想让第二个select案例中的第二个陈述和另一个案例中的第一个陈述匹配1对1,他可以简单地删除第二个select案例中的第二个陈述。但显然不熟悉VBA的人至少应该在回答中向他们解释这一点,我没有做到我知道。