2014-10-01 66 views
0

嘿,我真的很新vba編程,我有一個小問題。 我想將偏移量的列複製到新的工作簿和工作表中。將列vba複製到新工作表中,並用偏移量

我有複製功能工作,但我沒有得到我的newWorkbook偏移量。只是列他們自己。我想在新表中跳過行。

所以我想這是一個簡單的修復,但我不能弄明白。 :) 所以任何幫助將是非常有益的。 它是代碼中的行 Destination:= 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 
+0

除非這只是一個奇怪的副本,你有作爲範圍漂浮在那裏,我認爲這是爲你註釋的Dim targetColumn的一部分。 – user3271518 2014-10-01 12:01:45

+0

我沒有複製正確。對此很抱歉。 :) – Halle79 2014-10-02 12:32:36

回答

0

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

+0

謝謝!這個伎倆。 :) – Halle79 2014-10-02 12:31:29

相關問題