VBA Excel:插入一个新列,每列填写一个公式,其中立即向左列

我想插入一个新的列每隔一栏大约260次,那么我需要用一个公式填写新的列,左边的立即列。 这是我必须插入一个新的列:

Sub insert_column_every_other() For colx = 2 To 266 Step 2 Columns(colx).Insert Shift:=xlToRight Next End Sub 

但我卡在公式,它只是复制公式,我是需要的C3值改变,并参考左边的立即列(其他部分可能不是100%,我是新的VBA,所以所有的更正赞赏):

 Sub Repeat() For ColNum = 3 To 2000 Step 2 Range(Cells(2, ColNum), Cells(21, ColNum)).FormulaR1C1 ="=AVERAGE(OFFSET(C3,1,0,2,1))" Next ColNum End Sub 

我认为下一个代码应该做你想要的

 Sub insert_column_and_Formula() Dim colx As Long Dim H As Worksheet Set H = H3 'Replace H3 with the sheet that contains your data For colx = 2 To 266 Step 2 'Insert the Column' Call H.Columns(colx).Insert(Shift:=xlToRight) 'Put the formula in the new Column' H.Range(H.Cells(2, colx), H.Cells(21, colx)).FormulaR1C1 = "=AVERAGE(OFFSET(RC[-1],1,0,2,1))" Next colx End Sub 

希望这可以帮助你,任何问题,请让我知道