Cwala
Cwala

Reputation: 29

How to copy an existing worksheet into my active workbook?

I have 2 sheets, in one Sheet there are 2 buttons Browsefile and Openfile and one textbox TextBox1. I use the Browsefile button to select a file that I want to open and use the Openfile button to open the workbook.

The trouble is, it opens in a new workbook instead of adding it to my active workbook. How could I solve this?

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

Upvotes: 0

Views: 18352

Answers (1)

CustomX
CustomX

Reputation: 10113

Try this :) It will allow you to select the file and copy the first sheet at the end of your opened workbook.

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

Upvotes: 1

Related Questions