Excel - 在图表

时间:2016-02-08 16:01:08

标签: excel vba

我正在尝试在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

但显然这还不够。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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