将列vba复制到具有偏移量的新工作表

时间:2014-10-01 09:32:29

标签: excel vba

嘿,我对vba编程很新,我有一个小问题。 我试图将具有偏移量的列复制到新的工作簿和工作表。

我有复制功能可以工作,但我没有得到我的newWorkbook上的偏移量。只是他们自己的列。我想跳过新表中的第一行。

所以我想这是一个简单的修复,但我无法弄明白。 :) 所以任何帮助都会非常有帮助。 它是代码中的行         目的地:= targetSheet.Columns(ColumnNumbers(i))的

这是代码的其余部分。

    Sub columnCopyNEW()

' Copy columns to a new file
Dim BookToCopy As String, BookCopyTo As String

Dim Headers As Variant
Dim ColumnNumbers As Variant
Dim i As Long
Dim SourceColumnFind As Range
'Dim targetColumn 

As Range
Dim targetSheet As Worksheet, copySheet As Worksheet

BookToCopy = "book to be copied" 'copy FROM workbook
BookCopyTo = "book that will get the new columns" 'copy TO workbook

Application.ScreenUpdating = False 'Disables "Screen flashing" between 2 workbooks


Set copySheet = Workbooks(BookToCopy).Worksheets(1)
Set targetSheet = Workbooks(BookCopyTo).Worksheets(1)

Headers = Array("ProductBrand", "ProductCountryOfOrigin", "ProductCustomsTarifNumber", "ItemSEGName30", ItemEnumber) 'Headers from exportfile
ColumnNumbers = Array("E", "AD", "AE", "K", "F") 'array of columns to paste in.

For i = LBound(Headers) To UBound(Headers)
    With copySheet.Rows(1)
        Set SourceColumnFind = .Find(Headers(i), after:=.Cells(1, 1), MatchCase:=True)
    End With


    If Not SourceColumnFind Is Nothing Then
        Application.CutCopyMode = False
        copySheet.Activate
        'copy to new workbook and sheet
         ActiveSheet.Range(SourceColumnFind, ActiveSheet.Cells(Rows.count, SourceColumnFind.Column).End(xlUp).Address).Offset(2).Copy _
         Destination:=targetSheet.Columns(ColumnNumbers(i)) ' This is where i need to put offset(1)
    End If
Next i
 targetSheet.Activate
 Application.ScreenUpdating = True 'turns screenupdating on


End Sub

1 个答案:

答案 0 :(得分:0)

目的地:targetSheet.Range(ColumnNumbers(i) & 2)