Error : 'A Value used in the formula is of the wrong data type' with User Defined Function

后端 未结 1 696
旧巷少年郎
旧巷少年郎 2021-01-24 22:36

I am building a User Defined Function.

I get an error

A Value used in the formula is of the wrong data type

I am trying to b

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-24 23:28

    Assuming vesselCell and shopCell are both Range objects, Range(vesselCell) and Worksheets("Shop").Range(shopCell) are part of the problem.

    Make them Range, not Variant.

    vesselCell.AddComment comment
    shopCell.Value = comment
    

    Now, this code isn't legal in a UDF. Make your procedure a Sub procedure (remove the As Variant return type), and invoke it from other VBA code, or attach it to a shape or button's OnAction property.

    User-Defined Functions take inputs, compute a result, and then return that result to the calling cell: your code not returning anything is a strong indicator that a Function isn't appropriate here.

    0 讨论(0)
提交回复
热议问题