【发布时间】:2021-07-06 07:06:18
【问题描述】:
下面的代码将列值从一个特定工作簿(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
上面的代码显然不起作用,我尝试了几种不同的策略都无济于事。如果有人可以帮助我,那就太好了。谢谢
【问题讨论】:
-
如果您为每一列重新计算
nextCell,而不是对所有列使用相同的值,如果您的任何列在结束... -
@Cleanrider 使用
Application.Match()的一个相当未知的功能发布了一个可能的解决方案来一次获取各个标题的所有想要的列号(假设现有标题)。