Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 如何使VBA函数将If,then语句的结果输出到单元格?_Excel_Vba - Fatal编程技术网

Excel 如何使VBA函数将If,then语句的结果输出到单元格?

Excel 如何使VBA函数将If,then语句的结果输出到单元格?,excel,vba,Excel,Vba,这是我第一次做任何编码,所以我很抱歉,如果它是可怕的,可以更好。我的问题是,当我尝试使用这个函数时,我只是在单元格中得到一个#值错误,我不知道为什么。 程序应获取用户输入的任何信息,并决定此特定连接器是否适用于指定输入。连接器是否工作取决于可以使用哪些电缆以及用户输入的连接器类型 Function CONNECTOR1(Cable As Double, Connector As String, Row As Double) As String CableI = Worksheets("S

这是我第一次做任何编码,所以我很抱歉,如果它是可怕的,可以更好。我的问题是,当我尝试使用这个函数时,我只是在单元格中得到一个#值错误,我不知道为什么。 程序应获取用户输入的任何信息,并决定此特定连接器是否适用于指定输入。连接器是否工作取决于可以使用哪些电缆以及用户输入的连接器类型

Function CONNECTOR1(Cable As Double, Connector As String, Row As Double) As String
    CableI = Worksheets("Sheet1").Range("A4").String
    ConnectorI = Worksheets("Sheet1").Range("B4").String
    MaxdBLossI = Worksheets("Sheet1").Range("D4").Value
    ConnectorTypeI = Worksheets("Sheet1").Range("E4").String
    If IsEmpty(CableI) = True And IsEmpty(ConnectorI) = True And Not IsEmpty(MaxdBLossI) And Not IsEmpty(ConnectorTypeI) Then
        If ConnectorTypeI = Worksheets("Sheet1").Range("I" & Row).Value And Application.WorksheetFunction.CountIf(Range("N3:N500"), Cable) Then
            CONNECTOR1 = Connector
        End If
    ElseIf IsEmpty(ConnectorTypeI) = True And CableI = Cable Then
        CONNECTOR1 = Connector
    ElseIf CableI = "" Then
        If Application.WorksheetFunction.CountIf(Range("N3:N500"), Cable) Then
            CONNECTOR1 = Connector
        End If
    ElseIf ConnectorTypeI = Worksheets("Sheet1").Range("I" & Row).Value And CableI = Cable Then
        CONNECTOR1 = Connector
    Else
        CONNECTOR1 = Worksheets("Sheet2").Range("O" & Row).ClearContents
    End If
End Function
'The goal of this code is for it to decide based on which inputs the user has given, if a certain connector will satisfy the criteria
'Connectors only work for a specific number of cables, so this set of code is only for a connector that works with one cable
'CableI is a cable part number input by the user
'ConnectorI is a connector part number input by the user
'MaxdBLossI is an input amount of acceptable signal loss
'ConnectorTypeI is an input for the specific type of connector needed`

如果…那么
是一个控制流语句,它没有“输出”。你能澄清一下你的头衔吗?至于糟糕,请尝试该函数,应该会有帮助;-)没有
{Range}.String
这样的东西。从即时窗格(Ctrl+G)调用该函数,您将得到一个实际的VBA错误。还要注意,要从工作表单元格(即UDF)调用的函数不能修改任何其他单元格,因此
.ClearContents
不能是该函数所做的事情(更不用说
.ClearContents
不是一个函数,因此
Else
分支将返回值分配给…一个方法调用,而该方法调用不会编译。除了
CONNECTOR1=Connector
中的最后一个末端,所有If语句都只是返回输入函数的连接器参数的值。我希望If在满足条件时返回连接器的值。我希望函数在满足任何条件集时输出连接器变量。我不知道如何实现。谢谢大家的评论!