以常量为参数的函数
Function with constant as an argument
一些预定义的 VBA 方法和函数需要将特定常量传递给它们,例如:
Application.Calculation = xlCalculationAutomatic
.cells(i,j).End(xlUp).Row
.PpParagraphAlignment = ppAlignCenter
在这些片段中,常数是 xlCalculationAutomatic、xlUp 和 ppAlignCenter。
当调用 function/method 并要求填充参数时,VBE Intellisense 通常会提供 select 的有效常量下拉列表。
有没有办法用我自己的子程序和函数实现同样的事情?例如。在下面的例子中,参数 "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
您似乎在寻找 Enum
statement。在您的情况下,它可能看起来像这样:
Enum Margin
Row
Column
End Enum
' …
Function FindLast(ws As Worksheet, margin As Margin, iColRow As Long)
If margin = Row Then
…
End Function
IntelliSense 可以使用它,但您可能希望为您的枚举常量提供一个通用前缀(例如 mar
),以便于在 IntelliSense DropDown 框中选择它们。这就是为什么xlUp
有前缀 xl
。虽然我个人不太喜欢这种前缀。
在这种情况下,您还可以使用 Excel-Enum XlRowCol:
您似乎还缺少 'or' If sRow__Col = "Row" Then
Function FindLast(ws As Worksheet, sRowOrCol As XlRowCol, iColRow As Long)
If sRowOrCol = xlRows Then
FindLast = ws.Cells(ws.Rows.Count, iColRow).End(xlUp).Row
Exit Function
ElseIf sRowOrCol = xlCols Then
FindLast = ws.Cells(iColRow, ws.Columns.Count).End(xlToLeft).Column
Exit Function
Else
MsgBox "Invalid argument"
End If
End Function
一开始只改了该改的部分,方便OP;完成调整代码:
Function FindLast(ws As Worksheet, RowOrCol As XlRowCol, ColRow As Long) As Long
Select Case RowOrCol
Case xlRows: FindLast = ws.Cells(ws.Rows.Count, ColRow).End(xlUp).Row
Case xlColumns: FindLast = ws.Cells(ColRow, ws.Columns.Count).End(xlToLeft).Column
Case Else: MsgBox "Invalid RowOrCol argument"
End Select
End Function
一些预定义的 VBA 方法和函数需要将特定常量传递给它们,例如:
Application.Calculation = xlCalculationAutomatic
.cells(i,j).End(xlUp).Row
.PpParagraphAlignment = ppAlignCenter
在这些片段中,常数是 xlCalculationAutomatic、xlUp 和 ppAlignCenter。
当调用 function/method 并要求填充参数时,VBE Intellisense 通常会提供 select 的有效常量下拉列表。
有没有办法用我自己的子程序和函数实现同样的事情?例如。在下面的例子中,参数 "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
您似乎在寻找 Enum
statement。在您的情况下,它可能看起来像这样:
Enum Margin
Row
Column
End Enum
' …
Function FindLast(ws As Worksheet, margin As Margin, iColRow As Long)
If margin = Row Then
…
End Function
IntelliSense 可以使用它,但您可能希望为您的枚举常量提供一个通用前缀(例如 mar
),以便于在 IntelliSense DropDown 框中选择它们。这就是为什么xlUp
有前缀 xl
。虽然我个人不太喜欢这种前缀。
在这种情况下,您还可以使用 Excel-Enum XlRowCol:
您似乎还缺少 'or' If sRow__Col = "Row" Then
Function FindLast(ws As Worksheet, sRowOrCol As XlRowCol, iColRow As Long)
If sRowOrCol = xlRows Then
FindLast = ws.Cells(ws.Rows.Count, iColRow).End(xlUp).Row
Exit Function
ElseIf sRowOrCol = xlCols Then
FindLast = ws.Cells(iColRow, ws.Columns.Count).End(xlToLeft).Column
Exit Function
Else
MsgBox "Invalid argument"
End If
End Function
一开始只改了该改的部分,方便OP;完成调整代码:
Function FindLast(ws As Worksheet, RowOrCol As XlRowCol, ColRow As Long) As Long
Select Case RowOrCol
Case xlRows: FindLast = ws.Cells(ws.Rows.Count, ColRow).End(xlUp).Row
Case xlColumns: FindLast = ws.Cells(ColRow, ws.Columns.Count).End(xlToLeft).Column
Case Else: MsgBox "Invalid RowOrCol argument"
End Select
End Function