我正在嘗試創建一個子例程,提示用戶選擇工作簿,然後將選定工作簿的第一張工作表添加爲現有(活動)工作簿中的選項卡。然後命名新標籤「數據」。下面是我使用至今代碼:將工作表從另一工作簿加載到Excel中與VBA
"ws.Add" method or With Block not set.
任何幫助表示讚賞:
Sub getworkbook()
' Get workbook...
Dim ws As Worksheet
Dim filter As String
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
ws = Application.GetOpenFilename(filter, , caption)
ws.Add After:=Sheets(Sheets.Count)
ws.Name = "DATA"
End Sub
此代碼似乎並不奏效,並返回以下錯誤。
感謝,
類似的問題:http://stackoverflow.com/questions/3840628/creating-and-naming-worksheet-in-excel-vba – 2012-08-15 23:32:43