我有2张,在一张工作表中有2个按钮Browsefile
和Openfile
以及一个文本框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
答案 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