如何在公式中引入循环计数以跳过列

How to introduce loop numeration into a formula in order to skip columns

我是 Vba 的初学者。我有以下代码:

Sub addbdh()
Dim i As Integer
Dim n As Integer

Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select

n = Selection.Count

For i = 1 To n
Cells(3, i * 2 - 1).Formula = "=BDH(B" & i * 2 - 1 & ",A1 ,B1 ,Today())"
Next i
End Sub

最初的目的是在第 3 行的列中引入一个公式,但每次循环时都会跳过中间的一列。

然而,我意识到我的代码适用于刚刚描述的概念但转置了(跳过行以在 B 列的行中引入公式)。

由于列是用字母命名的,我不知道如何在公式 en 元素中引入循环来跳过列,就像我最初在指令中对行所做的那样:

Cells(3, i * 2 - 1).Formula = "=BDH(B" & i * 2 - 1 & ",A1 ,B1 ,Today())"

更具体地说,我不知道如何转换 "=BDH(B" & i * 2 - 1 & ",A1 ,B1 ,Today())" 以便每次跳过一列而不是一行。

有人可以帮助我吗?

非常感谢

在公式中使用索引值时,有时使用 R1C1 公式样式比使用 A1 样式更容易。

在您的情况下,您的 R1C1 公式可能是:

Cells(3, i * 2 - 1).FormulaR1C1 = "=BDH(RC" & i * 2 - 1 & ",R1C1 ,R1C2 ,Today())"

表示第一个参数 "cell in the same row as the current cell, (2*i-1)th column "、第二个 "cell in row 1 column 1 (aka A1)" 和最后一个 "cell in row 1 column 2 (aka B1)"。