2013-06-06 63 views
1

我想在單獨的實例中打開工作簿。目前,此工作簿已保存在桌面上。我想打開一個新的工作簿,這個工作簿沒有保存或位於我係統的任何位置。以下是我現在的代碼。請指教。創建一個未保存的空白工作簿

Sub New_Excel() 
    'Create a Microsoft Excel instance via code 
    'using late binding. (No references required) 
    Dim xlApp As Object 
    Dim wbExcel As Object 

    'Create a new instance of Excel 
    Set xlApp = CreateObject("Excel.Application") 

    'Open workbook, or you may place here the 
    'complete name and path of the file you want 
    'to open upon the creation of the new instance 
    Set wbExcel = xlApp.Workbooks.Open("C:\Users\PRASHPRA\Desktop\Book.xls") 

    'Set the instance of Excel visible. (It's been hiding until now) 
    xlApp.Visible = True 

    'Release the workbook and application objects to free up memory 
    Set wbExcel = Nothing 
    Set xlApp = Nothing 
End Sub 
+2

歡迎來到Stackoverflow。請花時間來正確格式化您的代碼;它使閱讀起來更容易。您可以通過粘貼,全選,然後單擊工具欄按鈕或按Ctrl + K來格式化它,或者將它自己縮進四個或更多空格。您可以在文本區域的正下方以所見即所得的視圖進行預覽,以便在發佈前查看它。如果您需要更多格式幫助,您可以點擊文本區域右上角的橙色「?」按鈕。如果我們可以閱讀你的代碼,你有更好的機會獲得helo。 :-) –

回答

3

如果你想創建一個新的空白工作簿,停止嘗試打開一個現有的。只要改變行

Set wbExcel = xlApp.Workbooks.Open("C:\Users\PRASHPRA\Desktop\Book.xls") 

'Add a new, empty workbook 
Set wbExcel = xlApp.Workbooks.Add 

欲瞭解更多信息,請參見Creating a New Workbook(該鏈接爲Excel 2003,因爲它是第一個我通過谷歌找到,但它仍然適用,如果你想要一個更近的鏈接,你可以像我那樣找到它)。

+0

+1不錯的答案@kenWhite – Santosh

相關問題