使用公式中的最后一行计数

时间:2015-01-05 17:25:00

标签: excel vba excel-vba

以下代码允许我重复从第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个活动行。

有没有办法可以替换单元格引用:公式中的$ A1932和$ L1932使用最后一行计数,因为每次报表运行时行数都会不同。

由于

2 个答案:

答案 0 :(得分:3)

使用&将允许您将变量连接到字符串中:

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

答案 1 :(得分:-1)

在我看来,您可以使用与计算lastRow的方式类似的变量。

示例(假设您在A栏中没有任何空白):

dim LookupLastrow as long

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

然后用"替换公式中的1932个引用; &安培; lookuplastrow& ":

.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),"" - "")"