返回列表 上一主題 發帖

[發問] 自定義函數如何賦值給儲存格

回復 1# dou10801


    謝謝前輩發表此主題與範例
後學研習自訂函數,測試方案如下,前輩參考

儲存格函數設定:


VBA:
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

本帖最後由 Andy2483 於 2023-5-23 16:37 編輯

回復 3# dou10801


    謝謝前輩回復
自訂函數好像只會給自己值,後學也是初學,前輩請參考以下的應用,看看適不適合

用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

本帖最後由 Andy2483 於 2023-5-24 08:23 編輯

回復 5# quickfixer
回復 3# dou10801

謝謝二位前輩
後學藉此帖學習到很多知識,懵懵懂懂學習心得如下,請前輩再指導




Option Explicit
Function MyCustomFunction(ByVal arg1 As Integer, ByVal arg2 As Integer) As String
'↑自訂函數:MyCustomFunction(宣告arg1是 長整數 ,宣告arg2是 長整數)
'↑因為[B2]本身自訂函數格 過程給[A1]值,而[B2]最終值是空白,所以不能是As Integer
'可以刪掉 As Integer變成通用型變數,或換宣告是 As String

Evaluate "test1(" & Application.Caller.Offset(-1, -1).Address(0, 0) & "," & arg1 & "," & arg2 & ")"
'↑回傳Evaluate "~~~" 值
'arg1變數傳遞給 a變數
'arg2變數傳遞給 b變數

MyCustomFunction = ""
'↑令這自訂函數結果是空白
End Function

Function test1(c As Range, a As Integer, b As Integer)
c = a + b
'↑a + b計算結果傳遞給c變數(儲存格)
End Function

Sub 儲存格位址()
MsgBox [A1].Address(0, 0)
MsgBox [A1].Address(1, 0)
MsgBox [A1].Address(0, 1)
MsgBox [A1].Address(1, 1)
End Sub
===============================================
以下是學習網頁
https://learn.microsoft.com/zh-t ... pplication.Evaluate
https://learn.microsoft.com/zh-t ... .application.Caller
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

本帖最後由 Andy2483 於 2023-5-24 11:28 編輯

回復 6# Andy2483


    更正:
Function MyCustomFunction(ByVal arg1 As Integer, ByVal arg2 As Integer) As String
'↑自訂函數:MyCustomFunction(宣告arg1是 短整數 ,宣告arg2是 短整數)結果值是字串

% -integer(整數資料類型_-32,768 到 32,767)
& -long(長整數_-2,147,483,648 到 2,147,483,647)
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 8# dou10801


    謝謝前輩回復
後學將結果變化如下,請參考

執行結果:



Function test1(c As Range, a As Integer, b As Integer)
c = "'" & a & b
End Function
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

本帖最後由 Andy2483 於 2023-5-24 15:26 編輯

回復 10# dou10801


    謝謝前輩再回復
後學研究如下方案,請前輩參考

執行結果:



Option Explicit
Function MyCustomFunction(ByVal arg1 As String, ByVal arg2 As String) As String
Evaluate "test1(" & Application.Caller.Offset(-1, -1).Address(0, 0) & ",""" & arg1 & """,""" & arg2 & """)"
MyCustomFunction = ""
End Function

Sub test1(c As Range, a As String, b As String)
c = a & b
End Sub
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 10# dou10801


    請前輩參考這帖:
http://forum.twbts.com/viewthrea ... %B9%AA%BA%B0%DD%C3D
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 13# dou10801


    謝謝前輩繼續一起學習
後學研習前輩的需求情境,學習方案如下,請前輩參考

Option Explicit
Function MyCustomFunction(ByVal arg1 As String, ByVal arg2 As String) As String
Evaluate "test1(工作表1!A1,""" & arg1 & """,""" & arg2 & """)"
MyCustomFunction = ""
End Function
Function test1(c As Range, a As String, b As String)
c = a & b
End Function
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復 16# 准提部林


    謝謝論壇,謝謝前輩指導

後學藉此帖學習前輩的方案,方案學習心得如下,請前輩再指導

執行結果:



Option Explicit
Function MyCustomFunction(arg1, arg2, V%) '自訂函數只給結果值, 不能指定存放位置
'↑自訂函數MyCustomFunction(),(arg1,arg2)宣告為通用型變數,V是短整數
Dim result
'↑宣告變數:result是通用型變數
If V = 1 Then result = Val(arg1) + Val(arg2)
'↑如果V變數是 1,就令result變數是 兩變數各轉為數值後相加值
If V = 2 Then result = arg1 & arg2
'↑如果V變數是 2,就令result變數是 兩變數相連接後的新字串
MyCustomFunction = result
'↑令MyCustomFunction函數回傳 result變數值
End Function

Sub TEST() '給自訂函數[參數],再指定填入位置, 須按鈕執行
Sheets("工作表2").[B2] = MyCustomFunction(100, 200, 1)
'↑令表2.[B2]儲存格值是 以arg1=100,arg2=200,V=1,
'給MyCustomFunction()函數回傳值

Sheets("工作表2").[B3] = MyCustomFunction("我的自訂", "函數", 2)
'↑令表2.[B3]儲存格值是 以arg1="我的自訂",arg2="函數",V=1,
'給MyCustomFunction()函數回傳值

End Sub
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

        靜思自在 : 人要知福、惜福、再造福。
返回列表 上一主題