Excel - 在图表的最后一列之后添加多列



我正在尝试在Excel中创建一个宏,该宏将在图表末尾添加三列,与图表中的前几列相同,其中一列增加了一列。

例如:

Column 1         Column 2   Column 3    Column 4         Column 5   Column 6
PGM Number 1     Amount     Notes       PGM Number 2     Amount     Notes

有没有办法在图表末尾添加三列(PGM 数字 +1、金额和注释),以便执行以下操作?

Column 7      Column 8   Column 9       Column 10      Column 11   Column 12
PGM Number 3     Amount     Notes       PGM Number 4      Amount       Notes

这是我必须复制最后一列的代码:

Sub InsertColumn()
Dim lastColumn As Long
lastColumn = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Column
Columns(lastColumn).Copy Destination:=Columns(lastColumn + 1)
End Sub

但显然这还不够。

感谢您的帮助!

Sub addCols()
Dim lastColumn As Long
Dim f As String
Dim s As String
Dim t As String
Dim c As Long
f = "PGM Number "
s = "Amount"
t = "Notes"
With ActiveSheet
    lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    c = (lastColumn + 3) / 3
    'assuming first row, otherwise change "1" to desired row
    .Cells(1, lastColumn + 1) = f & c
    .Cells(1, lastColumn + 2) = s
    .Cells(1, lastColumn + 3) = t
End With
End Sub

最新更新