将工作表从一个工作簿(未打开)复制到已打开的工作簿

时间:2017-04-17 07:48:10

标签: excel-vba copy vba excel

是否可以从工作簿中复制工作表而不打开它并将其粘贴到没有默认文件位置的已打开工作簿?我要复制的工作表包含总共30个工作表。我已经尝试使用文件对话框打开,但有时它会挂起excel。有没有什么方法可以让excel挂起并做我想做的事情?先感谢您。

Dim FileToOpen As String
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a Excel File to Open", _
FileFilter:="Excel Files *.(*.xlsx),xlsx")
If VarType(FileToOpen) = vbBoolean Then
MsgBox "No file selected", vbExclamation, "Sorry!"
Exit Sub
End If
Debug.Assert VarType(FileToOpen) = vbString
With Application.Workbooks.Open(FileToOpen)
.Worksheets(22).UsedRange.Copy ThisWorkbook.Worksheets(2).range("A1")
.Close
End With

1 个答案:

答案 0 :(得分:0)

Private Sub test()
 Dim app As New Excel.Application
 Dim fileName As String, copyFrom As Workbook
 app.Visible = False
 fileName = app.GetOpenFilename()

 Set copyFrom = app.Workbooks.Open(fileName)
  MsgBox copyFrom.Sheets(1).Cells(1, 1)

End Sub

这应该适合你想要实现的目标