繁体   English   中英

Excel VBA-将参数传递给函数

[英]Excel VBA - Passing argument to a function

我试图创建一个Excel函数,该函数将以我要求的任何形式加粗我告诉它的任何范围。 不幸的是,在正确地传递变量并获得此结果方面,我仅获得了部分成功。 当然,没有人喜欢其中的一部分,所以有人可以让我知道我在想什么。

Sub Macro1()
On Error Resume Next

'Create & reset testing area.
   Range("A1:C6").value = "A"
   Range("A1:C6").Font.Bold = False
   [b2].Select

'The two lines below call the function perfectly and the cells are bolded without issue
   Text_bold ([a1])
   Text_bold (Cells(2, 1))

'However, the party stops there as the following code errors out.
   Text_bold ([b1].Address)
   Text_bold (Selection)
   Text_bold (Range("B3"))
'Similarly, the below fails as well...
   Text_bold (Range("B4:C4"))
'And even less surprising, the following also refuses to assist in the endeavor...
   Text_bold (Application.Union(Range("B5:C5"), Range("B6:C6")))
End Sub

Function Text_bold(x As Range)
   'MsgBox VarType(x)
   x.Font.Bold = True
End Function

请帮忙。

函数参数周围的括号引起了问题。 它们强制将封闭的值作为函数参数传递之前进行评估,并传递Range.Value而不是Range对象。

Sub Macro1()
    On Error Resume Next

     'Create & reset testing area.
    Range("A1:C6").Value = "A"
    Range("A1:C6").Font.Bold = False
    [b2].Select

    'The two lines below call the function perfectly and the cells are bolded without issue
    Text_bold [a1]
    Text_bold Cells(2, 1)

    'However, the party stops there as the following code errors out.
    Text_bold Range([C1].Address)
    Text_bold Selection.Range
    Text_bold Range("B3")
    'Similarly, the below fails as well...
    Text_bold Range("B4:C4")
    'And even less surprising, the following also refuses to assist in the endeavor...
    Text_bold Application.Union(Range("B5:C5"), Range("B6:C6"))
    MsgBox "OK"
End Sub

如果您确实要使用括号,请在函数前加上Call语句。

Call Text_bold(Application.Union(Range("B5:C5"), Range("B6:C6")))

为了获得有关该问题的更多详细信息,您需要删除该语句
On Error Resume Next (又名执行On Error Hide All Bugs

删除后,我能够确定问题所在

  • 该函数(应为Sub,因为它不返回值)期望使用Range对象: Text_bold( x As Range )

  • Text_bold ([b1].Address)用括号不正确地调用它,并且它尝试发送一个字符串而不是范围作为参数

  • 您对该函数的所有调用均应使用方括号

尝试这个:


Sub Macro1()
    'Create & reset testing area.
    Range("A1:C6").Value = "A"
    Range("A1:C6").Font.Bold = False
    [b2].Select

    Text_bold [a1]
    Text_bold Cells(2, 1)
    Text_bold [b1]
    Text_bold Selection
    Text_bold Range("B3")
    Text_bold Range("B4:C4")
    Text_bold Application.Union(Range("B5:C5"), Range("B6:C6"))

    'A sub cannot return a value, a function can but it doesn't have to

    'To return a value from the Text_bold function
    Dim functionResponse As Boolean

    functionResponse = Text_bold([B3])  '<- THIS is where you need to use brackets
    MsgBox "Text in Cell [B3] is bold: " & functionResponse

End Sub

Function Text_bold(x As Range) As Boolean
    x.Font.Bold = True
    Text_bold = (x.Font.Bold = True)    'assign the return value to the function name
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM