繁体   English   中英

运行时错误'9':生成电子表格时下标超出范围

[英]Run-Time Error '9': Subscript Out of Range when generating spreadsheet

我目前正在尝试使用访问数据库,该数据库使用vba生成Excel电子表格和AutoCAD图形; 我没有编写代码,也没有使用这种语言编写代码的经验。 生成Excel文件时,代码到达MyXL.Parent.Windows(1).Visible = True ,它给出了错误。 excel文件已生成,但与模板相同。

文件和目录名称是占位符

Dim MyXL As Object

    FileCopy "\Directory\Template", "\Directory\Filename"
    ' This copies an Excel file, first half, then renames it with the Sales order number
    Set MyXL = GetObject("\Directory\Template")
    ' This opens the Excel file named in the upper code second half
    MyXL.Application.Visible = True
    MyXL.Application.WindowState = 3
          '  MyXL1.Activate
    MyXL.Parent.Windows(1).Visible = True
    MyXL.Parent.ActiveWindow.WindowState = 2

    With MyXL.Worksheets(1)

    End With

此时,它以.Range("T60").Value = Me![Text516]的形式设置了很多值(我假设) .Range("T60").Value = Me![Text516]

   MyXL.Worksheets(1).Activate
    MyXL.Save
MyXL.Parent.Quit ' This is what you have to do to close the Application
'MyXL.Parent.Quit
   ' MyXL.Parent.ActiveWindow.WindowState = xlMinimized
'    MyXL.Close

可能的重复与复制Excel电子表格有关,但是这个问题比那还要严重。

编辑:我犯了一个错误,以前有行Set MyXL = GetObject("\\SameDirectory\\SameFilename")但实际上是Set MyXL = GetObject("\\Directory\\Template")

工作代码示例打开一个Excel工作簿,进行编辑,保存为新名称。

Sub CopyExcel()
Dim xl As Excel.Application, xlw As Excel.Workbook
Set xl = CreateObject("Excel.Application")
'the following two lines have same result
Set xlw = xl.Workbooks.Open("C:\Users\June\MyStuff\Condos.xlsx", , True)
'Set xlw1 = xl.Workbooks.Add("C:\Users\June\MyStuff\Condos.xlsx")
'code to edit 
xlw.SaveAs "C:\Users\June\MyStuff\Condos2.xlsx"
xl.Quit
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM