dynamic column reference in formula

穿精又带淫゛_ 提交于 2020-01-07 09:03:15

问题


I'd like to place a formula 3 cells to the left of the last column. Is there a way to do this? Perhaps through a column address?

lc = .Cells(3, Columns.count).End(xlToLeft).Column
.Cells(3, lc + 3).Formula = "=CountCcolor(E3:N" & lrPT & ", " & .Cells(0, lc + 2) & "3)"

Regards,


回答1:


lc + 3 is three columns to the right, not the left but that is almost assuredly a typo. This should square your formula construction away.

lc = .Cells(3, Columns.count).End(xlToLeft).Column
.Cells(3, lc + 3).Formula = "=CountCcolor(E3:N" & lrPT & ", " & .Cells(3, lc + 2).ADDRESS & ")"

VBA's Range.Address property can output cell references in various combinations of relative and absolute addressing. I've left this as the default (e.g. absolute) but I do not believe it actually matters in your formula (single formula in single cell).

btw, if there are values in column N, the lc + 2 will be referencing P3, not O3.



来源:https://stackoverflow.com/questions/32081094/dynamic-column-reference-in-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!