首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在申请过程中按时传递变量?

如何在申请过程中按时传递变量?
EN

Stack Overflow用户
提问于 2019-07-04 18:58:03
回答 2查看 314关注 0票数 0

谁能解释一下如何在application.ontime过程中将for循环计数器x作为sub email_send的参数进行传递。

找到附件的代码,我写了发送电子邮件提醒的基础上的不同类型的绘图。

代码语言:javascript
复制
Dim x As Long

Sub drawings()
    lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

    For x = 2 To lastrow
        If Cells(x, "F") = "Type-1" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time1 = Now() + TimeValue("00:02:00")
            Application.OnTime time1, "'email_send" & x & "'"
        ElseIf Cells(x, "F") = "Type-2" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time2 = Now() + TimeValue("00:04:00")
            Application.OnTime time2, "'email_send" & x & "'"
        ElseIf Cells(x, "F") = "Type-3" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time3 = Now() + TimeValue("00:08:00")        
            Application.OnTime time3, "'email_send" & x & "'"
        ElseIf Cells(x, "F") = "Type-4" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time4 = Now() + TimeValue("00:10:00")        
            'time4 = time4 + 5        
            Application.OnTime time4, "'email_send" & x & "'"
            MsgBox time4
        End If

        MsgBox Cells(x, "A")        
    Next x
End Sub



Sub email_send(ByVal x As Long)
    Dim OutApp As Object       
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        'MsgBox "hello"
        .To = Cells(2, 9).Value
        .Subject = "Case ID " & Cells(x, "A") & " (" & Cells(x, "B") & ") Deadline Approaching"
        .Body = "Please complete your assigned drawing asap."
        .Display
        .Send
    End With
End Sub

我是VBA的新手,所以我请求你们找出我代码中的错误并建议编辑。

编辑:感谢您指导我完成这些步骤,但问题是当我遵循这些步骤时,我得到了一个意外的错误。我想附上错误的照片,但我不能,因为我没有足够的分数。

EN

回答 2

Stack Overflow用户

发布于 2019-07-04 19:10:36

当调用sub时,你可以传递参数。

代码语言:javascript
复制
Sub email_send(x as variant) 
'your code here
End sub
x = 3
email_send x

或者,您可以将x声明为公共变量:

https://docs.microsoft.com/en-us/office/vba/language/concepts/getting-started/declaring-variables

票数 0
EN

Stack Overflow用户

发布于 2019-07-04 20:42:59

首先,您需要更改email_send的签名,以便它包含一个接受Long...

代码语言:javascript
复制
Sub email_send(ByVal x As Long)

然后你可以像下面这样传递你的参数...

代码语言:javascript
复制
Application.OnTime time1, "'email_send " & x & "'"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56886475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档