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

Macros 如何让这个公式只填充空白单元格?,macros,autofill,Macros,Autofill,我试图在我设置的范围内,用这个公式只自动填充空白单元格 我已经测试过添加单元格。特殊单元格(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[

我试图在我设置的范围内,用这个公式只自动填充空白单元格

我已经测试过添加
单元格。特殊单元格(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

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

欢迎使用。我建议将来一定要为特定语言添加适当的标记。这对我来说显然是VB6(vba),但对其他人来说可能不清楚。清晰的标签将有助于接收答案

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

  • 查找所有可能为fileld的单元格
  • 仔细检查一下,看看是不是空的
  • 可以逐个填充单元格,也可以重新定义范围以仅包含非空单元格
  • 我将使用for循环使用“1-by-1”

    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
    ),但它应该可以做到这一点。

    欢迎这样做。我建议将来一定要为特定语言添加适当的标记。这对我来说显然是VB6(vba),但对其他人来说可能不清楚。清晰的标签将有助于接收答案

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

  • 查找所有可能为fileld的单元格
  • 仔细检查一下,看看是不是空的
  • 可以逐个填充单元格,也可以重新定义范围以仅包含非空单元格
  • 我将使用for循环使用“1-by-1”

    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
    ),但它应该可以做到这一点。

    它会出错。未定义用户定义的类型。用cell和cells加上我自己的lastrow代码进行测试似乎
    cells
    不起作用,但将
    dim i设置为range
    在测试后效果良好。编辑了我的答案。它确实有效,但它不会按P计算行数。它会自动填充,直到结束。只需转换我提供的代码,使用行计数器即可。我使用了另一种方法,这可能不是你要走的路(如我在回答中所述)。:-)它出错了。未定义用户定义的类型。用cell和cells加上我自己的lastrow代码进行测试似乎
    cells
    不起作用,但将
    dim i设置为range
    在测试后效果良好。编辑了我的答案。它确实有效,但它不会按P计算行数。它会自动填充,直到结束。只需转换我提供的代码,使用行计数器即可。我使用了另一种方法,这可能不是你要走的路(如我在回答中所述)。:-)