我下面的代码将列值从一个特定工作簿(Activeworkbook - 列 O、AH 和 I)转置到另一个工作簿(“loader file.xls” - 列 A、B、C)。它非常适合我的需求
Sub PullTrackerInfo()
'Pull info from respective column into correct column on loader file
Dim wb_mth As Workbook, wb_charges As Workbook, mapFromColumn As Variant, mapToColumn As Variant
Dim lastCell As Integer, i As Integer, nextCell As Integer, arrCopy As Variant
Set wb_mth = ActiveWorkbook
Set wb_charges = Workbooks("loader file.xls")
mapFromColumn = Array("O", "AH", "I")
mapToColumn = Array("A", "B", "C")
For i = 0 To UBound(mapFromColumn)
With wb_mth.Sheets(1)
lastCell = w.Sheets("owssvr").ListObjects("Table_owssvr").Range.Rows.Count
arrCopy = .Range(mapFromColumn(i) & 2 & ":" & mapFromColumn(i) & lastCell)
End With
With wb_charges.Worksheets(1)
nextCell = .Range(mapToColumn(i) & .Rows.Count).End(xlUp).Row + 1
.Range(mapToColumn(i) & nextCell).Resize(UBound(arrCopy), UBound(arrCopy, 2)).Value = arrCopy
End With
Next i
End Sub
我想做的是更进一步,我通常必须将数据排序到正确的列,以便将其转置到加载程序文件。我想做的是根据列标题的标题(“市场代码、“ID”、“C 代码”)移动列数据。请参阅下面的想法...
mapFromColumn = Array("Market Code", "ID", "C Code",
mapToColumn = Array("A", "B", "C")
For i = 0 To UBound(mapFromColumn)
With wb_mth.Sheets(1)
lastCell = w.Sheets("owssvr").ListObjects("Table_owssvr").Range.Rows.Count
arrCopy = .Range(mapFromColumn(i) & 2 & ":" & mapFromColumn(i) & lastCell)
End With
With wb_charges.Worksheets(1)
nextCell = .Range(mapToColumn(i) & .Rows.Count).End(xlUp).Row + 1
.Range(mapToColumn(i) & nextCell).Resize(UBound(arrCopy), UBound(arrCopy, 2)).Value = arrCopy
End With
Next i
End Sub
上面的代码显然不起作用,我尝试了几种不同的策略但无济于事。如果有人能帮助我那就太好了。谢谢