在函数中传递一个子函数名称作为参数

我有一个用户窗体在工作簿中有工作表名单的列表框,并有三个button:一个删除在列表框中select的所有工作表,一个只是清除这些工作表,另一个从该工作表中删除图表。 我目前有三个子函数看起来几乎相同,因为每个函数都基于所选button在循环中运行一个子函数:

Sub DeleteSheets() 'Removes sheets selected by user in ListBox For k = 0 To sheetNameListBox.ListCount - 1 If sheetNameListBox.Selected(k) = True Then DeleteSheet sheetNameListBox.List(k) End If Next k End Sub Sub ClearSheets() 'Clears sheets selected by user in ListBox For k = 0 To sheetNameListBox.ListCount - 1 If sheetNameListBox.Selected(k) = True Then ClearSheet sheetNameListBox.List(k) End If Next k End Sub Sub DeleteCharts() 'Removes charts from sheets selected by user For k = 0 To sheetNameListBox.ListCount - 1 If sheetNameListBox.Selected(k) = True Then DeleteChart sheetNameListBox.List(k) End If Next k End Sub 

为了解决这个问题,我决定开发一个这样的子function:

 Sub RemoveSheetData(removeType As String) For k = 0 To sheetNameListBox.ListCount - 1 If sheetNameListBox.Selected(k) = True Then If removeType = "DeleteSheet" Then DeleteSheet sheetNameListBox.List(k) If removeType = "ClearSheet" Then ClearSheet sheetNameListBox.List(k) If removeType = "DeleteChart" Then DeleteChart sheetNameListBox.List(k) End If Next k End Sub 

这似乎也是低效的,因为我必须每个都有三个案例。 有没有办法,我可以直接input我想调用的函数的名称,并运行?

这是从这里被公然窃取的答案

 Sub test() Dim ftnName As String Dim argument As String Dim result As String ftnName = "myFunction" argument = "cat" result = Application.Run(ftnName, argument) MsgBox result End Sub Function myFunction(inString As String) As String myFunction = inString & " has " & Len(inString) & " letters." End Function