我写了一个很小的宏,如果下一列中的单元格为空,则应该在一系列单元格上运行VLOOKUP公式。
我的问题是,我运行了For Each
循环并正常工作,但是在2次迭代之后,宏完成了。应该对每个单元进行检查。
Sub copyFormula()
Dim i As Long
Dim cell As Variant
i = 4
For Each cell In Sheets
cell = Range("B" & i)
If Not (IsEmpty(cell)) Then
Range("C" & i).FormulaR1C1 = "=VLOOKUP(RC[-2],Locations!C[-2]:C[-1],2,FALSE)"
Else: Range("B" & i).Offset(1, 0).Activate
End If
i = i + 1
Next cell
End Sub
这是工作簿的样子:
此代码将循环遍历激活工作表中range的每个单元格B4:B100
。如果为空,则将公式添加到column C
。
Sub copyFormula()
Dim cell As Range
For Each cell In ActiveSheet.Range("B4:B100")
If Not (IsEmpty(cell)) Then
cell.Offset(, 1).FormulaR1C1 = "=VLOOKUP(RC[-2],Locations!C[-2]:C[-1],2,FALSE)"
End If
Next
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句