一些预定义的VBA方法和函数需要传递一个特定的常量,例如:
Application.Calculation = xlCalculationAutomatic
.cells(i,j).End(xlUp).Row
.PpParagraphAlignment = ppAlignCenter
在这些代码片段中,常量为xlCalculationAutomatic、xlUp和ppAlignCenter。
当调用函数/方法并要求填写参数时,VBE Intellisense通常会提供一个下拉列表,供选择有效的常量。
是否有办法在自己的子例程和函数中实现相同的功能?例如,在下面的情况下,参数"sRowOrCol"需要用户当前输入字面量"Row"或"Col",但我想提供一个下拉列表,其中包含"xlRow"和"xlCol"等选项。
Function FindLast(ws As Worksheet, sRowOrCol As String, iColRow As Long)
If sRowOrCol = "Row" Then
FindLast = ws.Cells(ws.Rows.Count, iColRow).End(xlUp).Row
Exit Function
ElseIf sRowOrCol = "Col" Then
FindLast = ws.Cells(iColRow, ws.Columns.Count).End(xlToLeft).Column
Exit Function
Else
MsgBox "Invalid argument"
End If
End Function
If m = Margin.Row Then
也可以正常工作并且在我看来更好。 在您的示例中不起作用,因为您将参数命名为类型之后,会使智能感知混淆。 - GSerg