Excel VBA 所有单元格的循环函数

Excel VBA Loop function for all cells

您好,我有从波兰个人身份证号码中提取出生日期的功能。 但功能只适用于当前单元格。如何修改代码以执行 A2 中的所有单元格 - 工作表末尾。

B2 - 单元格代码

=IF(VALUE(MID(A2;3;2))>20;"20"&LEWY(A2;2);"19"&LEWY(A2;2))&MOD(MID(A2;3;2);20)&MID(A2;5;2)

我认为您正在尝试将公式从 B2 一直复制到 A 列中具有输入的单元格。我认为这应该可以满足您的需求。我假设它在 Sheet 1 上,如果不是,您可以更改代码。

'Defines the parameter to count the rows being used in column A
    lRow = Sheet1.Range("A2").End(xlDown).Row

'Creates the formula for column B
Sheet1.Range("B2") = "=IF(VALUE(MID(A2;3;2))>20;""20""&LEWY(A2;2);""19""&LEWY(A2;2))&MOD(MID(A2;3;2);20)&MID(A2;5;2)"

'Copies and paste the formulas down to the last rows for column B in Sheet 1
    Sheet1.Range("B2").Copy
    Sheet1.Range("B3:B" & lRow).PasteSpecial Paste:=xlPasteFormulas