2017-06-23 52 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