如何将现有工作表复制到我的活动工作簿?

时间:2012-10-10 12:11:14

标签: vba excel-vba excel

我有2张,在一张工作表中有2个按钮BrowsefileOpenfile以及一个文本框TextBox1。我使用Browsefile按钮选择要打开的文件,然后使用Openfile按钮打开工作簿。

问题是,它在新工作簿中打开,而不是将其添加到我的活动工作簿中。我怎么能解决这个问题?

Public fileStr As String
Sub GetOpenFile()

fileStr = Application.GetOpenFilename()
Worksheets("Sheet1").TextBox1.Value = fileStr

End Sub
Sub Paste_Click()
Dim wbk1 As Workbook, wbk2 As Workbook

Set wbk1 = ActiveWorkbook
Set wbk2 = Workbooks.Add(fileStr)

wbk2.Sheets(1).Cells.Copy wbk1.Worksheets("Sheet2").Cells(1, 1)

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个:)它将允许您选择文件并复制打开的工作簿末尾的第一张表。

Sub Paste_Click()
Dim wbk1 As Workbook, wbk2 As Workbook

fileStr = Application.GetOpenFilename()

Set wbk1 = ActiveWorkbook
Set wbk2 = Workbooks.Add(fileStr)

wbk2.Sheets("Sheet1").Copy After:=Workbooks("WorkbookNameYouCopyCodeInto").Sheets(3)
wbk2.Close
End Sub