Application.OnTime的多个变量参数 [英] Multiple Variable Arguments to Application.OnTime

查看:362
本文介绍了Application.OnTime的多个变量参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Excel 2010的数据采集前端. 我无法弄清楚将多个局部变量参数传递给Application.OnTime的语法.

I am working on a data acquisition frontend for Excel 2010. I can't figure out the syntax for passing multiple local variable arguments to Application.OnTime.

http://markrowlinson.co.uk/articles.php?id=10 为单个变量作为参数提供了一个很好的例子,但是我不清楚如何将其外推到多个变量的解释.

http://markrowlinson.co.uk/articles.php?id=10 provides a good example for a single variable as an argument, but the explanation on how to extrapolate this to multiple variables is not clear to me.

在传递多个局部变量参数时,是否有人对"'字符的正确嵌套有一个简洁的解释?

Does anyone have a concise explanation of the correct nesting of " and ' characters when passing multiple local variable arguments?

代码示例如下:Application.OnTime Now + TimeSerial(0, 0, 5), "'runScheduledReport """ & iArg1 & "","" & iArg2 & "" "" & iArg3 & "" ""'". 我知道我们将双引号用作字符串中的转义字符,但无法真正确定要传递的字符串的顺序.

Code example would be like this: Application.OnTime Now + TimeSerial(0, 0, 5), "'runScheduledReport """ & iArg1 & "","" & iArg2 & "" "" & iArg3 & "" ""'". I understand that we're using the double quote as an escape character within the string, but can't really figure out the ordering of the strings being passed.

推荐答案

您必须考虑以下约束:

  • 您要调用的宏必须驻留在模块中.当您想从另一个工作簿中调用它时,它必须是公开的.
  • 您不能像使用函数或带有参数的子程序那样使用方括号来调用宏.当使用方括号时,Excel会抱怨宏不存在
  • 我没有尝试过函数,但是无论如何,没有人可以使用返回值,因此将您的方法定义为一个子类.
  • 您必须使用撇号来封装宏名称
  • 您必须使用引号将字符串和日期值封装起来,或者使用chr $(34)(使我想起以前)或仅将引号加倍
  • 您可以传递不带引号的整数,我没有尝试过Doubles
  • 用逗号分隔参数
  • 参数的顺序必须与方法中参数的顺序匹配

查找随附的代码:

Option Explicit

Sub Test()
    Dim strTest1 As String
    Dim strTest2 As String

    strTest1 = "This is test1"
    strTest2 = "This is test2"

    Application.OnTime Now + TimeSerial(0, 0, 1), "'CallMeOnTime """ & strTest1 & """,""" & strTest2 & "'"
    Application.OnTime Now + TimeSerial(0, 0, 1), "'CallMeOnTime " & Chr$(34) & "Test" & Chr$(34) & "," & Chr$(34) & "Test" & Chr$(34) & "'"
    Application.OnTime Now + TimeSerial(0, 0, 1), "'CallMeOnTime2'"
End Sub
Public Sub CallMeOnTime(strTest1 As String, strTest2 As String)
    MsgBox ("test1: " & strTest1 & " test2:" & strTest2)
End Sub

Public Sub CallMeOnTime2()
    MsgBox ("CallMeOnTime2")
End Sub

这篇关于Application.OnTime的多个变量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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