使用公式中的最后一行数

下面的代码允许我重复从第2行到最后一个活动行的所有行的公式。

Dim LastRow As Long With Sheets("C PUR TYPE") LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("M2:M" & LastRow).formula = "=IFERROR(RIGHT(SUBSTITUTE(Lookup_concat(A2,$A$2:$A1932,$L$2:$L1932),"" 0,"",""""),LEN(SUBSTITUTE(Lookup_concat(A2,$A$2:$A1932,$L$2:$L1932),"" 0,"",""""))-2),"" - "")" End With 

在这种情况下,有1'932个活动行。

有没有一种方法可以replace单元格引用:公式中的$ A1932和$ L1932使用最后一行数作为每次报表运行时的行数。

谢谢

使用&将允许您将variables连接到string中:

 Dim LastRow As Long With Sheets("C PUR TYPE") LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("M2:M" & LastRow).formula = "=IFERROR(RIGHT(SUBSTITUTE(Lookup_concat(A2,$A$2:$A" & LastRow & ",$L$2:$L" & LastRow & "),"" 0,"",""""),LEN(SUBSTITUTE(Lookup_concat(A2,$A$2:$A" & LastRow & ",$L$2:$L" & LastRow & "),"" 0,"",""""))-2),"" - "")" End With 

在我看来,你可以使用类似于你计算lastRow的variables。

示例(假设A列中没有空格):

dim LookupLastrow as long

LookupLastrow=range("A1").end(xldown).row

然后用“&lookuplastrow&”replace公式中的1932年引用:

 .Range("M2:M" & LastRow).formula = "=IFERROR(RIGHT(SUBSTITUTE(Lookup_concat(A2,$A$2:$A" & lookuplastrow & ",$L$2:$L" & lookuplastrow & "),"" 0,"",""""),LEN(SUBSTITUTE(Lookup_concat(A2,$A$2:$A" & lookuplastrow & ",$L$2:$L" & lookuplastrow & "),"" 0,"",""""))-2),"" - "")"