使用VBA在Access中打开Excel文件 [英] Opening Excel files in Access using VBA

查看:802
本文介绍了使用VBA在Access中打开Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,每次我运行宏时,它都会在Access中冻结。  Excel文件名拼写正确,文件保存在正确的目录中。 最后,我需要打开以下excel文件进​​行修改。 非常感谢

Hi Guys, every time I run the macro, it freezes in Access.  The Excel file name is spelled correctly and the file is saved in the proper directory.  In the end, I need the following excel files to be open for modification.  Many thanks

推荐答案

您必须创建一个Excel.Application对象变量并进行设置:

You must create an Excel.Application object variable and set it:

Private Sub FixForeignDebtSwap_Click ()

    Dim xlApp As Excel.Application

    Dim Y As String,M As String,FileLoc As String

    Dim MyArray1 As Variant,i As Long,x As Excel.Workbook

    MyArray1 = Array(" xDebts.xlsx"," xDebtsCFs.xlsx"," xSwaps.xlsx",_

       " xSwapFixedLeg.xlsx"," xSwapFloatingLeg.xlsx")

Private Sub FixForeignDebtSwap_Click()
    Dim xlApp As Excel.Application
    Dim Y As String, M As String, FileLoc As String
    Dim MyArray1 As Variant, i As Long, x As Excel.Workbook
    MyArray1 = Array("xDebts.xlsx", "xDebtsCFs.xlsx", "xSwaps.xlsx", _
        "xSwapFixedLeg.xlsx", "xSwapFloatingLeg.xlsx")

    On Error Resume Next

   设置xlApp = GetObject(Class:=" Excel.Application")

    On Error GoTo 0

   如果xlApp什么都没有那么就是
       设置xlApp = CreateObject(" Excel.Application")

   结束如果是
    xlApp.Visible = True

   如果是IsNull(ReportDate)则为
        FileDir = Null

   否则

        Y =格式(ReportDate,"yyyy")

        M =格式(ReportDate," mmm")

        MainDir =" M:\ \\ tTRM\CM \ CorpocinFinance \ACFA Files \ALM \" &安培; Y& " \" &安培; M& " " &安培; Y& " \"

        FileLoc =" M:\ TRM \ CM \ CorporateAinance \ACFA Files \ALM \" &安培; Y& " \" &安培; M& " " &安培; Y& " \ data\"

   结束如果

    On Error Resume Next
    Set xlApp = GetObject(Class:="Excel.Application")
    On Error GoTo 0
    If xlApp Is Nothing Then
        Set xlApp = CreateObject("Excel.Application")
    End If
    xlApp.Visible = True
    If IsNull(ReportDate) Then
        FileDir = Null
    Else
        Y = Format(ReportDate, "yyyy")
        M = Format(ReportDate, "mmm")
        MainDir = "M:\TRM\CM\CorporateFinance\ACFA Files\ALM\" & Y & "\" & M & " " & Y & "\"
        FileLoc = "M:\TRM\CM\CorporateFinance\ACFA Files\ALM\" & Y & "\" & M & " " & Y & "\Data\"
    End If

   对于i = LBound(MyArray1)到UBound(MyArray1)

       设置x = xlApp.Workbooks.Open(FileLoc& MyArray1(i))

   下一个

结束子

    For i = LBound(MyArray1) To UBound(MyArray1)
        Set x = xlApp.Workbooks.Open(FileLoc & MyArray1(i))
    Next
End Sub


这篇关于使用VBA在Access中打开Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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