我有一个按钮,按下时调用一些宏来完成。 我的所有宏都运行良好,具有“AddDropDown”宏的异常。 它一直给我一个错误,上面写着“编译错误:参数不是可选的(错误449)”。
如果我手动选择它们并按“运行”,这些运行正常。
帮助页面显示“参数的数量和类型必须与预期的一致。此错误有以下原因和解决方案: •参数数量不正确。提供所有必要的论据。例如,Left函数需要两个参数;第一个表示正在操作的字符串,第二个表示从字符串左侧返回的字符数。因为这两个参数都不是可选的,所以必须都提供。 •省略的参数不是可选的。只有在过程声明中声明为Optional时,才能从对用户定义过程的调用中省略参数。要么在调用中提供参数,要么在定义中声明参数Optional。“
我的代码是这样的:
Private Sub Button_Click()
Macro1
Macro2
AddDropDowns
AddDropDown
Macro3
End Sub
提供错误的宏如下:
Sub AddDropDowns()
Dim cell As Range
Dim iDropDown As Long
With Worksheets("SourceSheet")
For Each cell In .Range("B13", .Cells(13, .Columns.Count).End(xlToLeft)).SpecialCells(XlCellType.xlCellTypeConstants)
AddDropDownEnroll Worksheets("DropDownsSheet"), iDropDown, cell.Offset(-1).Value, "='" & .Name & "'!" & cell.Resize(WorksheetFunction.CountA(cell.EntireColumn) - 1).Address
Next cell
End With
End Sub
Sub AddDropDown(sht As Worksheet, dropDownCounter As Long, header As String, validationFormula As String)
With sht.Range("A1").Offset(, dropDownCounter) '<--| reference passed sheet row 1 passed column
.Cells(1, 1) = header '<--| write header
With .Cells(2, 1).Validation '<--| reference 'Validation' property of cell 1 row below currently referenced one
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=validationFormula
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End With
dropDownCounter = dropDownCounter + 1
End Sub
答案 0 :(得分:2)
在Sub Button_Click()
中,更改:
Private Sub Button_Click()
Macro1
Macro2
AddDropDowns
AddDropDown
Macro3
End Sub
进入:
Private Sub Button_Click()
Macro1
Macro2
AddDropDowns
Macro3
End Sub
在AddDropDowns()
中,更改:
AddDropDownEnroll Worksheets("DropDownsSheet"), iDropDown, cell.Offset(-1).Value, "='" & .Name & "'!" & cell.Resize(WorksheetFunction.CountA(cell.EntireColumn) - 1).Address
成:
AddDropDown Worksheets("DropDownsSheet"), iDropDown, cell.Offset(-1).Value, "='" & .Name & "'!" & cell.Resize(WorksheetFunction.CountA(cell.EntireColumn) - 1).Address