How to insert a comment with the text of a cell through a function?
How to insert a comment with the text of a cell through a function?
This is the function code to add comment and optionally calculates something in the cell.
Option Explicit
Public Function AdicionarComentario(celula As Range, Optional Calcs)
On Error GoTo ErrHandler:
If TypeOf Application.Caller Is Range Then
Dim Caller As Range
Set Caller = Application.Caller
Caller.ClearComments
Caller.AddComment CStr(celula.Value)
End If
If Not IsMissing(Calcs) Then
AdicionarComentario = Calcs
Else
AdicionarComentario = ""
End If
Exit Function
ErrHandler:
' error handling code
AdicionarComentario = CVErr(xlErrNA)
On Error GoTo 0
End Function
By inserting the formula =AdicionarComentario(D1)
into E1
, add a comment with the text of cell D1
in cell E1
.
Byinsertingtheformula=AdicionarComentario(D2;SOMA(E3;E4))
intoE2
,addacommentwiththetextofcellD2
incellE2
.AnditusestheoptionalfieldwithSOMA(E3;E4)
,whereitsumsE3+E4andreturnsthevalueofthiscalculation.
Thedescriptioncanbeaddedbyrunningthiscodeonce.
SubDescreverFunction()DimFuncNameAsStringDimFuncDescAsStringDimCategoryAsStringDimArgDesc(1To2)AsStringFuncName="AdicionarComentario"
FuncDesc = "Adiciona comentário na célula e cálculos opcionalmente."
Category = 14
ArgDesc(1) = "Célula com o texto do comentário"
ArgDesc(2) = "Cálculo realizado na célula"
Application.MacroOptions _
Macro:=FuncName, _
Description:=FuncDesc, _
Category:=Category, _
ArgumentDescriptions:=ArgDesc
End Sub
With this, the window to insert the arguments of the function will have the description to facilitate the life of the user.