Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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_Cells - Fatal编程技术网

Excel 与细胞一起工作

Excel 与细胞一起工作,excel,vba,cells,Excel,Vba,Cells,我得到了以下简单的函数: Public Function AddFields(field1 As Range, field2 As Range) As String AddFields = "=" + field1.Address + "+" + field2.Address End Function Private Function GetCustomerCount(monthNumber As Integer) As Range If monthNumber < 6

我得到了以下简单的函数:

Public Function AddFields(field1 As Range, field2 As Range) As String
    AddFields = "=" + field1.Address + "+" + field2.Address
End Function


Private Function GetCustomerCount(monthNumber As Integer) As Range
    If monthNumber < 6 Then
        GetCustomerCount = Range("D13")
    ElseIf monthNumber < 12 Then
        GetCustomerCount = Range("D14")
    Else
        GetCustomerCount = Range("D15")
    End If
End Function
它不起作用。我得到以下错误:对象变量或未设置块。

在GetCustomerCount=Range(“D13”)上


为什么?

我认为您需要确定该范围适用于哪个工作表,即ActiveSheet.Range(“D13”)

GetCustomerCount是一个
范围

如果要为其指定新的值/范围,则需要使用set执行此操作

    Set GetCustomerCount = Range("D13")

在工具->选项中,您可能希望启用“requirevariable Declaration”-这在开始时有点不方便,但在开始出现奇怪的错误之前,请确保将所有的鸭子排成一行。Marg提到的是VBA的一个容易忘记的约定:“正常变量”如整数、长数和字符串通常用i=4或userName=“John Smith”赋值。其他任何东西、范围、工作表等都是对象,需要使用关键字“Set”,如Marg的示例所示。
    Set GetCustomerCount = Range("D13")