2017-06-23 142 views
2

我试图创建一个Excel函数,该函数将以我请求的任何形式将它告诉它的任何范围加粗。不幸的是,我只是在正确传递变量并获得这个结果方面取得了部分成功。当然,没有人喜欢一个部分,所以有人可以让我知道我错过了什么。Excel VBA - 将参数传递给某个函数

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 

请帮忙。

回答

2

函数参数周围的括号引起了问题。他们强制封闭值在作为函数参数传递之前进行评估,传递一个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"))) 
+1

更多的了解,我认为这是有帮助的注意,那些失败的行失败,因为Range对象提供Range.Value而不是作为对象本身 – serakfalcon

+0

@serafalcon传递,是指出。谢谢。 –

+1

另外,如果您尝试使用不带调用的圆括号调用多参数函数,则会出现语法错误,如:FunctionX(param1,param2) –

1

为了得到你需要删除的声明
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