excel中VBA如何在模块中自定义函数,定义好了之后如何在窗体中调用?谢谢 vba:如何在程序中调用自定义窗体
excel\u4e2dVBA\u5982\u4f55\u5728\u6a21\u5757\u4e2d\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5b9a\u4e49\u597d\u4e86\u4e4b\u540e\u5982\u4f55\u5728\u7a97\u4f53\u4e2d\u8c03\u7528\uff1f\u8c22\u8c22vb\u51fd\u6570\u5206sub\uff08\u65e0\u8fd4\u56de\u503c\uff09\u4ee5\u53cafunction\uff08\u6709\u8fd4\u56de\u503c\uff09
\u7b80\u5355\u8303\u4f8b
Public
Function
ss()
As
String
ss
=
"\u6211\u662f"
End
Function
\u6ce8\u610f\uff0c\u60f3\u5b9a\u4e49\u7684\u51fd\u6570\u80fd\u591f\u5728\u7a97\u4f53\u6216\u522b\u7684\u6a21\u5757\u8c03\u7528\uff0c\u8be5\u51fd\u6570\u5fc5\u987b\u662fpublic\u7684\uff0c\u4e5f\u5c31\u662f\u7b2c\u4e00\u4e2a\u5173\u952e\u5b57\u5fc5\u987b\u4e3apublic
function\u7684\u8c03\u7528\u5f88\u7b80\u5355\uff0c\u76f4\u63a5\u5199\u51fd\u6570\u540d\u548c\u53c2\u6570
\u5982
msgbox
ss
\u800csub\u7684\u8c03\u7528\u76f4\u63a5call\uff0c\u5982\uff1a
Public
Sub
ss()
ss
=
"\u6211\u662f"
End
Sub
\u8c03\u7528call
ss
\u53e6\u5916\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff0c\u8c03\u7528\u6a21\u5757\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u76f4\u63a5\u5199\u51fd\u6570\u540d\u3002\u4f46\u5982\u679c\u8c03\u7528\u7a97\u4f53\u7684\u51fd\u6570\uff0c\u5c31\u5f97\u628a\u7a97\u4f53\u52a0\u4e0a\u3002\u6bd4\u5982\uff0c\u6211\u8981\u8c03\u7528sheet1\u4e0b\u7684bb\u51fd\u6570\uff0c\u5219\u8be5\u5199\u6210
call
sheet1.bb
sub mysub()
\u81ea\u5b9a\u4e49\u7a97\u4f53.show vbmodal '\u663e\u793a\u6a21\u5f0f\u7a97\u4f53
Sheets(1).Range("A1")=\u81ea\u5b9a\u4e49\u7a97\u4f53.TextBox1.value
end sub
\u6700\u597d\u5728\u81ea\u5b9a\u4e49\u7a97\u4f53\u4e2d\u5b9a\u4e49\u4e2a\u516c\u6709\u53d8\u91cf\uff0c\u5728\u9000\u51fa\u7a97\u4f53\u65f6\u628aTextBox1.value\u503c\u5b58\u5165\u8be5\u53d8\u91cf\uff0c\u53ef\u80fd\u662f\u7a97\u4f53\u88ab\u5378\u8f7d\u540e\u6587\u672c\u6846\u4e2d\u7684\u6570\u636e\u4e0d\u4fdd\u7559\uff0c\u4f46\u53d8\u91cf\u4e2d\u7684\u6570\u636e\u4fdd\u7559\u7684\u3002
简单范例
Public Function ss() As String
ss = "我是"
End Function
注意,想定义的函数能够在窗体或别的模块调用,该函数必须是public的,也就是第一个关键字必须为public
function的调用很简单,直接写函数名和参数
如 msgbox ss
而sub的调用直接call,如:
Public Sub ss()
ss = "我是"
End Sub
调用call ss
另外一点需要注意,调用模块定义的函数,直接写函数名。但如果调用窗体的函数,就得把窗体加上。比如,我要调用sheet1下的bb函数,则该写成 call sheet1.bb
在模块里定义:
function aa(a)
aa=a
end function
窗体里调用:
x=aa(1)
msgbox x
绛旓細妯″潡鍙橀噺鏄鍦ㄦā鍧鍐鐨杩囩▼澶栧畾涔夌殑鍙橀噺锛岀敤dim private public閮藉彲浠ワ紝涓嶈繃鐢╬ublic瀹氫箟鐨勬槸鍏ㄥ眬鍙橀噺锛屾槸鍦ㄥ叾瀹冩ā鍧椾篃璧蜂綔鐢ㄧ殑鍙橀噺锛屼篃灏辨槸璇村湪鍏跺畠妯″潡涔熷彲浠ュ紩鐢ㄨ鍙橀噺銆傝宻tatic瀹氫箟鐨勬槸闈欐佸彉閲忥紝涓鑸殑杩囩▼鍐呭彉閲忓湪杩囩▼缁撴潫鍚庡嵆涓嶅瓨鍦ㄤ簡锛岃岀敤static瀹氫箟鐨勫垯鍦ㄨ繃绋嬬粨鏉熷悗浠嶇劧淇濆瓨鍦ㄥ唴瀛樹腑锛屽苟淇濈暀鍏跺師...
绛旓細Excel涓浣跨敤VBA鍒朵綔涓涓伓浣滃墽灏忕▼搴忕殑鎿嶄綔姝ラ濡備笅锛氶鍏堟垜浠墦寮涓涓伐浣滄牱琛ㄤ綔涓轰緥瀛愩備娇鐢╝lt+f11缁勫悎蹇嵎閿繘鍏be缂栬緫鍣紝鎻掑叆涓涓柊妯″潡锛屽苟鍦ㄦā鍧椾腑杈撳叆浠ヤ笅浠g爜锛歄ption Explicit Sub 鎭朵綔鍓()Dim Num Num = InputBox("閫夋嫨1-5浠绘剰涓涓暟瀛楁祴璇曚綘鐨勬ф牸")MsgBox (Choose(Num, "瀹", "鑵",...
绛旓細x澹版槑涓哄叏灞鍙橀噺锛鍦ㄦā鍧椾腑澹版槑浜哖ublic x As String灏卞ソ浜
绛旓細vba妯″潡:鏄彲浠ュ紩鐢ㄥ伐浣滆〃閲岀殑瀛愮▼搴忕殑銆傜浉浜掓槸鍖归厤鐨
绛旓細鍦ㄦā鍧椾腑鐨绗竴琛屽姞涓婁笅闈竴鍙 Application.Volatile 杩欐牱浣犳寜F9灏卞彲浠ユ洿鏂版暟鎹簡
绛旓細鍦╒B涓畾涔塃XCEL瀵硅薄锛岀劧鍚庢墦寮EXCEL鏂囦欢锛屽氨鑳界敤EXCEL瀵硅薄璋冪敤EXCEL VBA浠g爜銆
绛旓細2銆鍦╡xcel宸ヤ綔琛ㄤ腑杈撳叆搴忓垪鍙峰緢绠鍗曪紝鍦╝1锛宎2鍗曞厓鏍间腑杈撳叆鏁板瓧1鍜2鍚庯紝浣跨敤濉厖鍔熻兘涓嬫媺鍗冲彲瀹屾垚搴忓垪鍙风殑杈撳叆銆3銆佸彲鏄綋鎴戜滑閬囧埌杈撳叆澶ч噺搴忓垪鍙风殑鏃跺欏氨浼氬緢楹荤儲锛岄渶瑕佹嫋鍔ㄧ殑璺濈澶暱锛岄偅涔堟垜浠彲浠ヤ娇鐢vba鏉ヨВ鍐宠繖涓棶棰樸傛垜浠娇鐢╝lt+f11缁勫悎蹇嵎閿繘鍏be缂栬緫鍣紝鎻掑叆涓涓柊鐨勬ā鍧楋紝骞鍦ㄦā鍧椾腑杈撳叆...
绛旓細鍦ㄦā鍧楃殑鏈椤朵笂锛岀敤 Public 澹版槑锛岀劧鍚庡湪鎵鏈夌殑妯″潡銆佺獥浣撱乻heet涓兘鍙互鐩存帴浣跨敤 渚嬪涓嬮潰鐨3涓ā鍧椾娇鐢ㄧ殑鏄悓涓涓彉閲廰Str锛屽湪浠讳綍涓涓ā鍧楃殑sub銆乫unction涓兘鍙互鏀瑰彉 aStr 鐨勫硷紝鎵浠ユ棤璁虹偣鍑籹heet涓殑鎸夐挳锛岃繕鏄痜orm涓殑鎸夐挳锛岄兘浼氫娇 aStr 鐨勫瓧绗︿覆涓嶆柇鍔犻暱 妯″潡1锛--- '涓涓叕鍏卞彉閲 Public ...
绛旓細Excel涓浣跨敤vba鍑芥暟鍒朵綔涔濅節涔樻硶琛ㄧ殑鎿嶄綔姝ラ濡備笅锛氶鍏堟垜浠墦寮涓涓猠xcel宸ヤ綔鏍疯〃浣滀负渚嬪瓙銆傚湪寮鍙戝伐鍏锋爣绛鹃潰鏉夸笅鎵撳紑vba缂栬緫绐楀彛锛屾垨鑰呬娇鐢╝lt+f11蹇嵎閿墦寮vba缂栬緫绐楀彛銆傜偣鍑绘彃鍏ヤ竴涓柊鐨勬ā鍧楋紝骞跺畾涔夋ā鍧梥ub Jiujiu()銆傛帴涓嬫潵鍦╦iujiu妯″潡涓杈撳叆浠ヤ笅 Sub nine9()Dim i As Byte, j As Byte, s As...
绛旓細浠婂ぉ涓哄ぇ瀹朵粙缁鍦╡xcel涓浣跨敤vba鏉ュ紩鐢ㄥ崟鍏冩牸鍖哄煙鏂规硶锛屼笉浼氱殑鏈嬪弸鍙互鍙傝冩湰鏂囷紝鏉ョ湅鐪嬪惂锛佹楠 1銆侀鍏堟垜浠墦寮涓涓伐浣滄牱琛ㄤ綔涓轰緥瀛愩2銆佷娇鐢╝lt+f11缁勫悎蹇嵎閿繘鍏be缂栬緫鍣紝鎻掑叆涓涓柊鐨勬ā鍧楋紝骞鍦ㄦā鍧椾腑杈撳叆浠ヤ笅浠g爜锛歄ption Explicit Sub Random()Dim myRange As Range Dim rng As Range '璁剧疆...