我试图让 VBA 中的子函数调用另一个函数,该函数返回一个范围并设置为一个变量。当我尝试运行 GetInputs() 方法时出现语法错误。
Function GetDataRange(str As String) As Range
' This prompts the user to select a range of data, we will need to call this once for inputs and once for outputs
Dim rRange As Range
On Error Resume Next
Application.DisplayAlerts = False
Set rRange = Application.InputBox(Prompt:= _
str, _
Title:="SPECIFY RANGE", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If rRange Is Nothing Then
Exit Function
Else
rRange.Font.Bold = True
End If
GetDataRange = rRange
End Function
Sub GetInputs()
Dim rg As Range
Set rg = GetDataRange("Select Inputs:")
End Sub
编辑:我添加了这段代码:
Sub Test()
End Sub
当我尝试运行它时,我得到相同的语法错误,并突出显示了 Sub Test() 行。
第一眼看去,你错过了Set
范围赋值语句中的关键字:
GetDataRange = rRange
相反,它应该是:
Set GetDataRange = rRange
Updated我已经运行了代码,这是我观察到的唯一错误。这应该可以解决它。
就我个人而言,我会避免在函数体内执行此操作:
rRange.Font.Bold = True
相反,将其放入您的调用例程中:
Sub GetInputs()
Dim rg as Range
Set rg = GetDataRange("Select Inputs:")
If Not rg Is Nothing Then rg.Font.Bold = True
End If
这样,您使用该函数主要是为了获取返回值,而不是对对象执行操作。但这主要是一个偏好问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)