参数不是可选错误

时间:2016-05-11 19:49:36

标签: excel vba excel-vba

我正在构建一张工作表来从我需要按位置和BU排序和过滤的职位空缺列表中提取数据。我需要代码来计算开口数,然后将该信息传递回主子,以用于创建其他页面并循环子。我一直在这段中得到上述错误。对我做错了什么的想法?

Sub Organize_Data()

    Dim A As Integer
    Dim B As Integer
    Dim C As Integer

    Worksheets.Add().Name = "Calculations"
    Find_Unit
    Find_Locations
    Count_BU_Data
    Count_Country_Data
    Count_Raw_Data
End Sub

Function Count_BU_Data(A As Integer)

   ActiveWorkbook.Worksheets("Calculations").Range("B3", Worksheets("Calculations").Range("B3").End(xlDown)).Rows.Count
End Function

1 个答案:

答案 0 :(得分:3)

你的UDF:

Function Count_BU_Data(A As Integer)

采用您未被指定为 Optional 参数的参数(A As Integer)。您可以在不提供此参数的情况下从其他例程调用该函数:

Sub Organize_Data()

    Dim A As Integer
    Dim B As Integer
    Dim C As Integer

    Worksheets.Add().Name = "Calculations"
    Find_Unit
    Find_Locations
    Count_BU_Data    '// <~~ No argument passed to function.
    Count_Country_Data
    Count_Raw_Data
End Sub
因此,&#39;论证不是可选的&#39;错误。

看到该函数似乎没有实际使用该参数,您可以从函数头中删除它:

Function Count_BU_Data() As Long '// Note I've included a return value...

或者将其作为可选参数

Function Count_BU_Data(Optional A As Integer) As Long

如果未提供可选参数

,您也可以指定默认值
Function Count_BU_Data(Optional A As Integer = 1) As Long