【问题标题】:Call excel function in access vba在access vba中调用excel函数
【发布时间】:2020-07-13 18:58:13
【问题描述】:

我在 excel 中有一个子程序,需要从访问权限中调用。 Excel vba

 Public Function testme(value As String) As String

 Dim xlpath As String
 Dim concate As String

 xlpath=ActiveWorkbook.Path
 value = ActiveWorkbook.Name
 concate = xlpath & "\" & value     
 Let testme = concate

 End Function

我需要在其中一种访问方法中调用上述方法。我该如何调用它。

 Sub Connect1()
 Dim xlApp As Variant
'Set xlApp = CreateObject("Excel.Application")
'this will launch a blank copy of excel; you'll have to load workbooks
'xlApp.Visible = True

Set xlApp = GetObject(, "Excel.Application")
    Let ans = xlApp.Application.Run("MyXLVBAProject.MyXLVBAModule.testme", 400)
'here ans has the string "500"
End Sub

【问题讨论】:

  • access是如何打开excel工作簿的?
  • 您可能希望从 Excel 的对象模型中使用 Application.Run。您向它传递一个字符串,例如“QuickRDA.JavaCallBacks.GetQuickTab”作为宏名称,其中 QuickRDA 是 Excel VBA 项目的名称,JavaCallBacks 是该 VBA 项目中 VBA 模块的名称,GetQuickTab 是该 VBA 模块中的函数。
  • 如果可以的话请给我代码示例
  • 在上面显示的代码示例中,您将 flname 参数作为字符串传递,然后为其分配另一个值,将其清除。 (此外,它也不会被使用。)此外,您省略了将返回值分配给函数 passfilename。也许您希望 passfilename = ActiveWorkbook.Path 返回路径?
  • 好的,我看到你的更新了。在我看来,您不需要从访问权限将参数传递给testme。所以,我建议你把value从形参改成局部变量。您也可以在调用中省略实际参数。我将更新我的答案,向您确切说明我的意思。

标签: excel ms-access vba


【解决方案1】:

您可能希望从 Excel 的对象模型中使用 Application.Run。您向它传递一个字符串,例如“QuickRDA.JavaCallBacks.GetQuickTab”作为宏名称,其中 QuickRDA 是 Excel VBA 项目的名称,JavaCallBacks 是该 VBA 项目中 VBA 模块的名称,GetQuickTab 是该 VBA 模块中的函数。

访问中

Sub Connect()    
    Dim xlApp As Variant
    Set xlApp = GetObject(, "Excel.Application")
    'this will connect to an already open copy of excel, a bit easier for quick & dirty testing
    Let ans = xlApp.Application.Run("MyXLVBAProject.MyXLVBAModule.testme")
End Sub

在 Excel 中

Public Function testme() As String
    Dim xlpath As String
    Dim concate As String
    Dim value as String
    xlpath = ActiveWorkbook.Path
    value = ActiveWorkbook.Name
    concate = xlpath & "\" & value     
    Let testme = concate
End Function

-或者简单地说-

Public Function testme() As String
    Let testme = ActiveWorkbook.FullName
End Function

请记住,在 Excel 中,函数 testme 应放在名为 MyXLVBAModule 的模块中,并且包含该模块的项目应称为 MyXLVBAProject。

【讨论】:

  • 非常感谢。我会尝试并告诉你。
  • 但我想通过文件名代替上面代码的 400。**ans** 没有返回文件名
  • 好的,当然,您可以传递您的字符串而不是 400,并且不要忘记在 testme 中将 value as Long 更改为 value as String(以及返回感兴趣的字符串)课程)。另外,请查看我对上述问题的第二条评论。
  • 我已经根据您的代码更改了我的代码。请看看我的问题。但是我应该通过什么而不是 400?请帮助我。我需要通过 concate 才能访问。
【解决方案2】:

那么,您想从 Access 触发 Excel 函数,还是从 Access 运行 Excel 子例程?

要运行一个函数,你可以这样做。

  Public Function FV(dblRate As Double, intNper As Integer, _ 
                  dblPmt As Double, dblPv As Double, _ 
                  intType As Integer) As Double
     Dim xl As Object
     Set xl = CreateObject("Excel.Application")
     FV = xl.WorksheetFunction.FV(dblRate, intNper, dblPmt, dblPv, intType)
     Set xl = Nothing
 End Function

要从 Access 运行 Excel 子例程,您可以执行以下操作。

Sub RunExcelMacro()
Dim xl As Object
'Step 1:  Start Excel, then open the target workbook.
   Set xl = CreateObject("Excel.Application")
    xl.Workbooks.Open ("C:\Book1.xlsm")
'Step 2:  Make Excel visible
   xl.Visible = True
'Step 3:  Run the target macro
   xl.Run "MyMacro"
'Step 4:  Close and save the workbook, then close Excel
   xl.ActiveWorkbook.Close (True)
    xl.Quit
'Step 5:  Memory Clean up.
   Set xl = Nothing 
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多