Excel VBA 将范围复制到新工作簿中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19325865/
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-08 16:53:10  来源:igfitidea点击:

Excel VBA Copy a Range into a New Workbook

excelvbaexcel-vba

提问by user2872317

I am a newbie to Excel VBA.

我是新手Excel VBA

I am trying to copy a range of data from worksheet output into a new excel workbook and save the new workbook with the the value in E3.

我正在尝试将工作表输出中的一系列数据复制到新的 excel 工作簿中,并使用 E3 中的值保存新工作簿。

As a bonus, I would love to be able to also copy the data into wordpadand save as E3.xml

作为奖励,我希望能够将数据复制wordpad并保存为 E3.xml

Here is the VBAI have thus far:

这是VBA我迄今为止的:

Sub CopyOutput()
Dim myname As String
mystring = E3
Dim myselection As Range

myselection = Sheets("Output").Columns("F").Select
Set NewBook = Workbooks.Add
    With NewBook
        .SaveAs Filename:="C:\Program Files\White Plume\Scenarios\" & myname & ".xls", FileFormat:= _
             xlsx, CreateBackup:=False
    End With
myselection.Paste
End Sub

回答by Lance Roberts

Modify to suit your specifics, or make more generic as needed:

修改以适合您的具体情况,或根据需要进行更通用:

Private Sub CopyItOver()
  Set NewBook = Workbooks.Add
  Workbooks("Whatever.xlsx").Worksheets("output").Range("A1:K10").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
  NewBook.SaveAs FileName:=NewBook.Worksheets("Sheet1").Range("E3").Value
End Sub