VBA代码将多个列从一个工作表导入到另一个工作表

时间:2017-07-28 20:59:43

标签: excel vba excel-vba

我正在尝试从一个Excel电子表格导入几个列到另一个。以下是我在此论坛上找到的代码,该代码工作正常。我面临的唯一问题是它只导入一列。

有人可以指导我如何编辑代码以便我可以导入多个列吗?

Importing single Column
Sub CopyColumnToWorkbook()
Dim sourceColumn As Range, targetColumn As Range

Set sourceColumn = Workbooks("Source.xlsm").Worksheets("2017"). Columns(“A”)
Set targetColumn = Workbooks("Target.xlsm").Worksheets("Field WH Projections").Columns(“A”)

sourceColumn.Copy Destination:=targetColumn
End 

1 个答案:

答案 0 :(得分:0)

根据未连接列的OP要求进行更新

Sub ColumnCopy()
Dim Source_Sheet As Worksheet, Target_Sheet As Worksheet
Dim CLM As Integer, LastColumn As Integer

Set Source_Sheet = Workbooks("Source.xlsm").Worksheets("2017")
Set Target_Sheet = Workbooks("Target.xlsm").Worksheets("Field WH Projections")

LastColumn = Source_Sheet.Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column

For CLM = 1 To LastColumn

'Columns 1 or (A)...  2 (B).... F and H.
If CLM = 1 Or CLM = 2 Or CLM = 6 Or CLM = 8 Then

Source_Sheet.Columns(CLM).Copy Destination:=Target_Sheet.Columns(CLM)

End If

Next CLM

End Sub