vba 需要使用 Excel 宏将 H、K、L 列从一个 Excel 工作簿复制到新工作簿

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

Need to copy columns H,K,L From one excel workbook to new workbook using Excel Macro

excelexcel-vbacopyvba

提问by bhargav reddy

I have a excel workbook A.xlsx with columns A through T, now i need to copy specific columns H,K,L to a new workbook which would be created while i run a macro.

我有一个包含 A 到 T 列的 excel 工作簿 A.xlsx,现在我需要将特定的 H、K、L 列复制到一个新的工作簿中,该工作簿将在我运行宏时创建。

I was able to successfully copy a range of columns from one worksheet to another, but i am not finding a way to copy specific columns to a new workbook.

我能够成功地将一系列列从一个工作表复制到另一个工作表,但我没有找到将特定列复制到新工作簿的方法。

Private Sub copy_sub()
Sheets("Sheet1").Columns("H:K").Copy Sheets("Sheet2").Range("A1")
End Sub

回答by Gary's Student

Give this a try:

试试这个:

Sub dural()
    Dim r1 As Range, r2 As Range
    Sheets("Sheet1").Select
    Set r1 = Range("K:L")
    Set r2 = Range("H:H")
    Set wbNew = Workbooks.Add
    r2.Copy Range("H1")
    r1.Copy Range("K1")
End Sub

Based on the fact that after the workbook is added, the new workbook will be active and Sheet1 will be the active sheet in that book. Also assumes that cols H,K,L are to be copied, but not col I.

基于添加工作簿后,新工作簿将处于活动状态,而 Sheet1 将成为该工作簿中的活动工作表。还假设要复制 cols H,K,L,但不复制 col I。

回答by Santosh

Try below sample code

试试下面的示例代码

   Private Sub copy_sub()

    Dim wkb As Workbook
    Set wkb = Workbooks.Add    ' Will add new workbook

    ' with column name
    ThisWorkbook.Sheets("Sheet1").Columns("H").Copy wkb.Sheets("Sheet2").Range("A1")

    'with column index
    ThisWorkbook.Sheets("Sheet1").Columns(9).Copy wkb.Sheets("Sheet2").Range("A1")

End Sub