簡體   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