[英]Excel VBA - Custom Function; #VALUE error; VLOOKUP on different worksheet
[英]Create a custom worksheet function in Excel VBA
我有一個微弱的記憶,能夠使用VBA函數來計算Excel中的值,像這樣(作為單元格公式):
=MyCustomFunction(A3)
可以這樣做嗎?
編輯:
這是我的VBA功能簽名:
Public Function MyCustomFunction(str As String) As String
該函數位於ThisWorkbook
模塊中。 如果我嘗試在工作表中使用它,如上所示,我得到#NAME?
錯誤。
解決方案(謝謝,代碼):定義ThisWorkbook
模塊時無法訪問該功能。 它必須位於“正確”模塊中,該模塊已手動添加到工作簿中。
是的,它可以。 您只需在模塊中定義VBA函數。 有關示例的詳細介紹,請參閱http://www.vertex42.com/ExcelArticles/user-defined-functions.html 。
這是一個簡單的例子:
Option Explicit Function MyCustomFunction(input) MyCustomFunction = 42 + input End Function
A1: 2 A2: =MyCustomFunction(A1)
輸入一詞需要替換,因為它是一個基本關鍵字。 請嘗試使用num。 您還可以通過指定類型(例如變體)來進一步發展。
Function MyCustomFunction(num As Variant)
MyCustomFunction = 42 + num
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.