簡體   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