VBA:使用Range.Formula右侧位置的数组索引

大家都知道在这一点上,你可以做下面的事情,把一个公式应用到一个范围,右边的单元格引用会dynamic更新:

Range("A1:A10").Formula = "=J2+M2" 

这不是我所要求的。 我试图将一个数组索引传递到Range.Formula的右侧,我没有得到我想要的结果。 对于初学者来说,这里是我的代码和左侧的工作正常(介意你是因为这是从MS Access启动):

 ' Get the new amount of columns since new ones have been added lastColumn = xl.Cells(1, xl.Columns.Count).End(xlToLeft).Column ' Create and array of the header names to quickly locate column number cols = xl.Range(xl.Cells(1, 1), xl.Cells(1, lastColumn)).Value ' Apply the formulas xl.Range(xl.Cells(2, xl.Match("FCode", cols, 0)), xl.Cells(lastRow, xl.Match("FCode", cols, 0))).Formula = 

这适用于正确的范围。 如果我把"ASD""=J2+M2"放在右边,它会更新正确的范围。 当我需要在右边使用xl.Match(...)作为公式的一部分时,问题就出现了。

例如:

 "=ISBLANK(" & xl.Range(xl.Cells(2, xl.Match("NCode", cols, 0)), xl.Cells(2, xl.Match("NCode", cols, 0))) & ")" 

返回1004: Application-defined or object-defined error 。 在应用范围内应该返回=ISBLANK(K2)=ISBLANK(K3) etc.

只是一个简单的引用,应该等于=K2=K3etc结束了等于单元格K2的值。 例如, =14 ,它适用于整个范围。 它甚至不会返回K3K4 etc的值。 这是用于这个的公式:

 "=" & xl.Range(xl.Cells(2, xl.Match("PCode", cols, 0)), xl.Cells(2, xl.Match("PCode", cols, 0))) 

我究竟做错了什么? 这样做的原因是因为我需要引用报告之间的更改的列,但标题名称保持不变。 在一个报告中可能是M列,而在另一个报告中可能是Z列。 到目前为止,我一直在使用For Loops ,但他们很慢,想避免它们。

 "=ISBLANK(" & xl.Range(xl.Cells(2, xl.Match("NCode", cols, 0)), _ xl.Cells(2, xl.Match("NCode", cols, 0))) & ")" 

应该可能是

 "=ISBLANK(" & xl.Cells(2, xl.Match("NCode", cols, 0)).Address(false, false) & ")"