如何用参数调用button的function
我对Excel很陌生,很困惑,所以请原谅基本的问题。 我有一个完美的自定义VBAfunction:
Public Function MissingSequence(Rng As Range) As String Dim iCnt As Integer 65 'ASCI character for "A" Dim iNum As Integer For iNum = 100 To 110 Dim sCheck As String sCheck = Chr(iCnt) & iNum If Rng.Find(sCheck, lookat:=xlWhole) Is Nothing Then Dim sMissingNumbers As String sMissingNumbers = sMissingNumbers & "," & sCheck End If Next MissingSequence = Mid(sMissingNumbers, 2) End Function
我目前正在使用=MissingSequence(D4:D10)
在单元格内调用此函数。 不过,我想将这个函数调用改为一个button。
当我用下面的代码创build一个命令button时:
Public Sub CommandButton1_Click() MissingSequence(D4:D10) End Sub
…然后按下button,没有任何反应。 在VBA中debugging时,出现语法错误 。
当我尝试这个…
Public Sub CommandButton1_Click(D4:D10) MissingSequence End Sub
…并再次点击button,没有任何反应。 debugging时,我得到了错误预期:列表分隔符或) 。
我现在完全丧失了如何使用button来使这个函数工作,并感觉我已经尝试了一切可能,所以我非常感谢一些帮助。
在你的代码中,你正试图使用一个命令
MissingSequence(D4:D10)
D4:D10
在这种情况下, D4:D10
意味着什么(事实上, D4
和D10
是variables,函数/子程序,而:
是命令分隔符),因此需要使用Range
函数将其转换为范围types。
在范围所在的表单上引用任何范围前缀也会更好。
Public Sub CommandButton1_Click() 'As this is a function that is being called, you need to catch the response. 'Failing to do so will cause the compiler to think that is a subroutine 'being called, and the `()` will force the parameters to be passed as values. Range("B1") = MissingSequence(Range("D4:D10")) '/ This should be preferred. Sheet1 is just the example name. Range("B1") = MissingSequence(Sheet1.Range("D4:D10")) End Sub