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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12819045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:04:10  来源:igfitidea点击:

How to copy an existing worksheet into my active workbook?

vbaexcel-vbaexcel

提问by Cwala

I have 2 sheets, in one Sheet there are 2 buttons Browsefileand Openfileand one textbox TextBox1. I use the Browsefilebutton to select a file that I want to open and use the Openfilebutton to open the workbook.

我有2片,在一个表有2个按钮BrowsefileOpenfile和一个文本框TextBox1。我使用该Browsefile按钮选择要打开的文件并使用该Openfile按钮打开工作簿。

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

回答by CustomX

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