如何让这个公式只填充空白单元格?

How do I get this formula to fill in only blank cells?

我正在尝试使用此公式在我设置的范围内仅自动填充空白单元格。

我测试过添加 cells.specialcells(xlCellTypeBlanks) 但它不起作用。

Sub test()

    Dim lastRw As Long
    Dim Rng As Range

    lastRw = Cells(Rows.Count, "P").End(xlUp).Row
    Set Rng = Range("Q1:Q" & lastRw)
    Rng = "=IFERROR(VLOOKUP(RC[-1],R1C1:R1C14,14,FALSE),"""")"
    Rng.Value = Rng.Value

End Sub

我希望它只能在空白单元格上粘贴公式。

欢迎来到 SO。我建议将来确保为特定语言添加适当的标签。这对我来说显然是 VB6 (vba),但对其他人来说可能不清楚。清晰的标签将有助于获得答案。

我会尽量保持简单。一个想法:

  1. 找到所有可能是 fileld 的单元格
  2. 遍历它们并找出它们是否为空
  3. 1 对 1 填充单元格或重新定义范围以仅包含 none 个空单元格。

我将使用 for 循环“一对一”。

sub test()

    dim fillRange as range
    dim lastRow as long
    dim i as range 'instead of cell / cells

    lastRow = activesheet.usedrange.rows.count 'Alternative for finding last row
    set fillRange = range("Q1:Q" & lastRow)
    for each i in fillRange 'i will be 1 cell in filLRange at a time
        'check if the current cell is empty (="") or contains nothing (isnull). If it does, insert formula
        if i = "" or isnull(i) then i = "=IFERROR(VLOOKUP(RC[-1],R1C1:R1C14,14,FALSE),"""")"
    next i
    calculate 'Make sure the formula takes effect

end sub

可能有一些拼写错误,因为我是在脑子里乱写的。 (尤其是行数,如果失败就使用你自己的。i as cell 可能也必须是 i as cells。)但它应该可以解决问题。