插入一列,然后将公式放在空白列中500次

时间:2018-08-13 01:28:50

标签: excel vba

这是我正在使用的代码

  oauth2RequestFactory = new DefaultTokenRequestFactory(myClientDetailsService);

在下一行中出现错误

Sub insert_column_every_other()
Dim colx As Long
Dim H As Worksheet
Set H = Sheets("Sheet1") 'Replace H3 with the sheet that contains your data
For colx = 9 To 1200 Step 2
    Call H.Columns(colx).Insert(Shift:=xlToRight)
    H.Range(H.Cells(2, colx), H.Cells(21, colx)).FormulaR1C1 = "= 
((OFFSET(RC[-1])-(OFFSET(RC[-3]))/(OFFSET(RC[-3]))*SQRT(252))"
Next colx
End Sub

该错误是应用程序定义的错误。

宏将在第9列之后创建第500列,然后在空白列中创建一列,然后在空白列中计算连续两天股价的百分比差,因此这就是我使用该特定抵消公式的原因。

1 个答案:

答案 0 :(得分:0)

我认为这就是您要寻找的

Option Explicit

Sub insert_column_every_other()

Dim ws As Worksheet: Set ws = Sheets("Sheet1")
Dim i As Long

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
    For i = 9 To 1200 Step 2
        ws.Columns(i).Insert
        ws.Range(ws.Cells(2, i), ws.Cells(21, i)).Formula = "=(" & ws.Cells(2, i - 1).Address(False, False) & "-" & ws.Cells(2, i - 2).Address(False, False) & ")/(" & ws.Cells(2, i - 2).Address(False, False) & ")*SQRT(252)"
    Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub