分享

VBA语句Function

 c857084163 2023-03-23 发布于浙江

【4】VBA公共变量调用Function法     

1/czm ok!

Public Qyh As Integer  '此句必须有

Public Qym1 As String

Sub ggBl1() '公共变量声明和使用 ok!

  Call QyhQym1(Qyh, Qym1)

  Call t1

  Call t2

  'Call confunction

End Sub

Function QyhQym1(Qyh, Qym1)

 Qyh = InputBox("企业号")

 y = Year(Now)

  'Debug.Print y

 m = Month(Now)

 m1 = m - 1

    'Debug.Print m1

 ym1 = "/" & y & "-" & m1

 Qym1 = Qyh & ym1

  'Debug.Print Qym1

     'Range("B2") = hym1

     'Qym1 = "1a/2019-05"

    'Cells(100, 1) = Qyh  '将企业号,存入当前工作表的单元格中

End Function

Sub t1()

 'Debug.Print Qym1

 a1 = Qym1 & "t1"

 Debug.Print a1

End Sub

Sub t2()

 a2 = Qym1 & "t2"

 Cells(6, 1) = a2

  Debug.Print a2

 Cells(6, 2) = Qym1

  Debug.Print Range("b6")

End Sub

----

Public Qyh As Integer

Public Qym1 As String

Sub ggBl1() '公共变量声明和使用 ok!

  'Call QyhQym1(Qyh, Qym1)

  Call QyhQym2(Qyh, Qym1)

  Call t1

  Call t2

  'Call confunction

End Sub

Sub QyhQym2(Qyh, Qym1)

 Qyh = InputBox("企业号")

 y = Year(Now)

  'Debug.Print y

 m = Month(Now)

 m1 = m - 1

    'Debug.Print m1

 ym1 = "/" & y & "-" & m1

 Qym1 = Qyh & ym1

End Sub

Function QyhQym1(Qyh, Qym1)

 Qyh = InputBox("企业号")

 y = Year(Now)

  'Debug.Print y

 m = Month(Now)

 m1 = m - 1

    'Debug.Print m1

 ym1 = "/" & y & "-" & m1

 Qym1 = Qyh & ym1

  'Debug.Print Qym1

     'Range("B2") = hym1

     'Qym1 = "1a/2019-05"

    'Cells(100, 1) = Qyh  '将企业号,存入当前工作表的单元格中

End Function

Sub t1()

 'Debug.Print Qym1

 a1 = Qym1 & "t1"

 Debug.Print a1

End Sub

Sub t2()

 a2 = Qym1 & "t2"

 Cells(6, 1) = a2

  Debug.Print a2

 Cells(6, 2) = Qym1

  Debug.Print Range("b6")

End Sub

【3】VBA宏自定义Function    

1/VBA宏自定义Function参数_闻达  http://blog.sina.com.cn/s/blog_499cff3f0100nteb.html

【2】VBA:Function,function  

21/让VBA自定义函数返回多个值  http://m./icontent/147579083547.html

20/Excel 如何让自定义函数返回多个值

 http://club./thread-457516-1-1.html

19/VB中如何调用有多个返回值的函数

https://sa93g4./pages/question/question?qid=1737439476413206347&hostname=baiduboxapp&_swebfr=1

18/vb自定义函数可选参数如何设置_百度知道  https://zhidao.baidu.com/question/571975263.html?qbl=relate_question_0&word=%A2%F5BA%D6%D0function%BA%AF%CA%FD%D6%D0%B2%CE%CA%FD

  BBS:

  function multi(a as integer,b as integer,optional third)

rem 定义multi函数,参数a、b为整形,参数third可可选参数

dim n as integer 

n = a * b

if not ismissing (third) then  '判断可选参数third是否有值传入 

 n = n * third

end if

multi = n

end function

private sub command1_click()

dim a as integer,b as integer

dim x as integer

a = 2 : b = 3

x = multi (a , b , 2)

print x

end sub

17/VB里面定义function函数设的参数到底有什么用_百度知道  https://zhidao.baidu.com/question/1495833879400724739.html?fr=iks&word=%A2%F5BA%D6%D0function%BA%AF%CA%FD%D6%D0%B2%CE%CA%FD&ie=gbk

16/vb用function怎样自定义函数?格式怎么写?举个例子,比如inputbox函数用function怎么写?_百度知道  https://zhidao.baidu.com/question/591845415.html?qbl=relate_question_0&word=%A2%F5BA%D6%D0confunction%BA%AF%CA%FD

15/VB函数function的用法_百度知道  https://zhidao.baidu.com/question/484043514.html?qbl=relate_question_2&word=%A2%F5BA%D6%D0confunction%BA%AF%CA%FD

14/https://zhidao.baidu.com/question/43425289.html?qbl=relate_question_2&word=%A2%F5BA%D6%D0confunction%BA%AF%CA%FD

Function是指方法(即函数bai)Function是有返回值的如下面的定义了一du个叫S的函数,返回String类型的字串(注意,如果zhi没有设置返回值,则默认为NULL)Private Function S() As String '//返回值为String类型…… '//函数主体S = "1234" '//返回的字串内容为"1234"End Function而Sub是过程(和函数一样,但是不能有返回值)如下面的定义了一个叫A的过程Private Sub S() '//不能设置返回类型,即无返回值…… '//过程主体End Sub

13/'定义函数

https://zhidao.baidu.com/question/43425289.html?qbl=relate_question_2&word=%A2%F5BA%D6%D0confunction%BA%AF%CA%FD

  Function fan(x As Single) As Double

    fan = 3 * x + 3

    End Function

12/function用来定义函数 

https://wenku.baidu.com/view/0b13c1d96f1aff00bed51e06.html

  function用来定义函数,一般一个函数放在一个.m 文件里。如建一个myfunction.m,然后在文件中写:

  function y=myfunction(a,b)

  '其中a,b 是输入函数的参数,y是函数返回的值。当需返回多个值时,可将y看作一个数组,或直接将函数的开头写成:function [x,y]=myfunction(x,y)的形式。

  然后定义函数内容,即怎样由输入参数a,b,得到返回值y。

  如y=a+b:

  function y=myfunction(a,b)

    y=a+b;

  保存为一个 m 文件myfunction.m,调用方式如下。

    num=myfunction(3,4)'由函数y=a+b,得num=3+4=7。

12.1.两个函数要存放在两个文件中

func1 存在func1.m 中

test 存在 test.m 中

  Matlab 的函数寻址是在工作路径下查找与函数名相同的文件.

  如果将两个函数

  function [a b]=func1(x,sigma)

a=x^2+sigma;

b=2*x+sigma;

function test(func,x)

sigma=1

f=@(x) func(x,sigma);

[a?b]=f(x);

放在一个文件中,如果不命名成test.m,那么则无法寻址到test这个函数;如果命

名为test.m,那么func1被作为test函数来调用,内部定义的test函数被认为是重

复定义的函数,不会被调用.

2.调用test ( @func1,2)的时候前括号用的中文括号,应改成英文的.

比如定义 f(x)=x^2,可以写为

f=@(x)(x.^2)

其中@(x)(x.^2)就是匿名函数,第一个括号里面是自变量,第二个括号里面是表

达式,@是函数指针

f=@(x)(x.^2)表示将匿名函数@(x)(x.^2)赋值给 f,于是f 就表示该函数。

于是f(2)=2.^2=4;f(1:3)=[1:3].^2=[1 4 9等] 等

定义匿名函数时也可以调用别的匿名函数,比如

f1=@(x,y)(x.^2+y.^2)

定义了函数 x^2+y^2

f2=@(t)(f1(t,2))

定义了函数 t^2+4

f3=@(x)(f1(x(1),x(2)))

定义了函数 x(1)^2+x(2)^2

使用匿名函数时一定要注意函数本身的参数形式,如

f1(2,3)

表示2^2+3^2

f2(3)=3

表示3^2+4

f3([1,2])

表示1^2+2^2

说白了就是函数指针

------

11/Function()函数 

https://sa93g4./pages/squestion/squestion?qid=1759559622564159148&rid=2844716703&titleScroll=128&eid=20430&_swebfr=1

调用必须返回执行结果,而 Proceduren 过程的调用是不用返回值。例如:要利用函数计算两个整数的加运算,并返回计算的结果。定义函数

  Functon AddRet(x as integer ,y as integer)

   AddRet=x+y

   End Function

   z=AddRet(6,8) '调用函数

 当执行上面的命令后,z 值=14,而过程就直接执行了定义全程变量 z定义执行过程

 Sub AddRet(x as integer ,y as integer)

  z=x+y

  End Sub

  调用过程Call AddRet(6,8)查看变量 z 的值应该是 14

10) Excel VBA自定义function函数作为公共变量功能调用的方法

Public Qyh As Integer

  Public Qym1 As String

Sub ggBl1() '公共变量声明和使用

  Call QyhQym1(Qyh, Qym1)

  Call t1

  Call t2

  'Call confunction

End Sub

Function QyhQym1(Qyh, Qym1)

 Qyh = InputBox("企业号")  '

 y = Year(Now)

  'Debug.Print y

 m = Month(Now)

 m1 = m - 1

    'Debug.Print m1

 ym1 = "/" & y & "-" & m1

 Qym1 = Qyh & ym1

  'Debug.Print Qym1

     'Range("B2") = hym1

     'Qym1 = "1a/2019-05"

    'Cells(100, 1) = Qyh  '将企业号,存入当前工作表的单元格中

End Function

Sub t1()

 'Debug.Print Qym1

 a1 = Qym1 & "t1"

 Debug.Print a1

End Sub

Sub t2()

 a2 = Qym1 & "t2"

 Cells(6, 1) = a2

  Debug.Print a2

 Cells(6, 2) = Qym1

  Debug.Print Range("b6")

End Sub

-------

9)https://zhidao.baidu.com/question/324580486.html

 Function gs()

gs = ActiveSheet.Cells(ActiveCell.Row, 1) + ActiveSheet.Cells(ActiveCell.Row, 2) + ActiveSheet.Cells(ActiveCell.Row, 3)

 End Function

 在D列及其右边列输入 =gs()

 2、Function gs(a, b, c)

gs = a + b - c '把公式的计算过程写到这里,输入公式时只要传入参数

End Function

 在D1输入 =gs(A1,B1,C1)

 复制填充公式

  3、其实要实现 作表中在录入数据后目标单元格自动计算,但不需要别人看见目标单元格中的公式

 可用工作表保护配合单元格锁定来解决

8)怎样在一个EXCEL的VBA里调用另一个EXCEL里的自定义VBA函数

https://zhidao.baidu.com/question/593812284.html?qbl=relate_question_4&word=VBA%D6%B1%BD%D3%B5%F7%D3%C3function

7)Excel VBA Function函数 传参问题

https://zhidao.baidu.com/question/570235220.html?qbl=relate_question_0&word=vba%BA%EA%D6%AE%BC%E4%D4%F5%C3%B4%B5%F7%D3%C3%B2%CE%CA%FD

6)要传参的话是不是应该用Function定义函数而不是Sub?

https://zhidao.baidu.com/question/429226193554767452.html?qbl=relate_question_6&word=vba%BA%EA%D6%AE%BC%E4%D4%F5%C3%B4%B5%F7%D3%C3%B2%CE%CA%FD

5E1)使用 If...Then...elseif...Else 语句(VBA)、Function

http://www./home/show.aspx?id=9798

【1】

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多