将子例程转换为函数 [英] Convert subroutine to function

查看:42
本文介绍了将子例程转换为函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将子例程转换为函数?我需要通过一个论点.

How to convert a subroutine to function? I need to pass an argument.

Sub maJolieProcedure()
    With Worksheets("employes").Range("A:A")
        Set c = .Find(what:="Smith")
        If Not c Is Nothing Then
            firstAddress = c.Row
            Worksheets("employes").Rows(firstAddress).Copy _
    Destination:=Worksheets("rapport").Range("A1")
            MsgBox "Ok"
        Else
            MsgBox "Nok"
        End If
    End With
End Sub

有效.

Function executerMaJolieProcedure(Texte As String) As String

    '   e.g. executerMaJolieProcedure('Smith')

    With Worksheets("employes").Range("A:A")
        Set c = .Find(what:=Texte)
        If Not c Is Nothing Then
            firstAddress = c.Row
            Worksheets("employes").Rows(firstAddress).Copy _
    Destination:=Worksheets("rapport").Range("A1")
            MsgBox "Ok"
        Else
            MsgBox "Nok"
        End If
    End With
End Function

不起作用.

其他问题...执行功能后,如何将重点放在员工"表上而不显示空白单元格?:)

Additional question ... How to give focus to the "employees" sheet instead of displaying the empty cell after executing the function? :)

感谢您的帮助.

推荐答案

您不需要函数,需要带有参数的 Sub :

You don't need a function, you need a Sub with an argument:

Sub MAIN()
    Dim s As String
    s = "whatever"
    maJolieProcedure s
End Sub


Sub maJolieProcedure(Texte As String)
    With Worksheets("employes").Range("A:A")
        Set c = .Find(what:=Texte)
        If Not c Is Nothing Then
            firstAddress = c.Row
            Worksheets("employes").Rows(firstAddress).Copy _
    Destination:=Worksheets("rapport").Range("A1")
            MsgBox "Ok"
        Else
            MsgBox "Nok"
        End If
    End With
End Sub

(VBA中的函数旨在仅返回值,而不修改工作表单元格.)

这篇关于将子例程转换为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆