复制将匹配的列从一个工作表粘贴到另一个工作表

时间:2019-12-03 14:00:11

标签: vba loops import range match

我有一个问题,关于将特定的列从一个工作表(ws_Copy)复制粘贴到另一个工作表(ws_Dest)。我需要遍历每个列标题并将其与ws_Copy中的标题匹配。如果匹配,则需要将整个列与匹配的标头复制到ws_Dest中。

不确定我的解释是否清楚,这是示例代码:

Sub Import_data()
Dim path As String
Dim file As String
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lastcol As Integer
Dim lastrow_ir As Integer
Dim i As Integer
Dim m As Integer

path = ThisWorkbook.Sheets("MACROS").Range("import_path_IRHedge").Value
file = ThisWorkbook.Sheets("MACROS").Range("file_IRHedge").Value

'Set variables for copy and destination sheets
Set wsCopy = Workbooks(file).Sheets("data_ir_bpv")
Set wsDest = ThisWorkbook.Sheets("IR Hedge")

'Find last used column in the destination sheet
'Find last row in the copy sheet 
lastcol = wsDest.Range("C:AD").Column
lastrow_ir = wsCopy_ir.Range("C" & Rows.Count).End(xlUp).Row

'Use the Match function to find the corresponding column to copy paste
For i = 3 To lastcol
    Set Rng1 = wsCopy_ir.Range("C14:AC14")
    m = Application.WorksheetFuntion.Match(wsDest.Cells(1, i).Value, Rng1, 0)
    wsCopy_ir.Range("C14:C & lastrow_ir").Columns(m).Copy
    wsDest.Range("C2").Columns(i).PasteSpecial xlPasteValues

Next i
Application.CutCopyMode = False
MsgBox "Data has been imported successfully"
End Sub

但是错误消息“对象不支持此属性或方法”始终显示在行中:

 m = Application.WorksheetFuntion.Match(wsDest.Cells(1, i).Value, Rng1, 0)

任何帮助将不胜感激!

0 个答案:

没有答案
相关问题