找到所有公式并将它们向下拖动(FillDown)到某个行号,跳过包含值的单元格?

时间:2014-08-19 03:40:30

标签: excel vba excel-vba excel-2007 excel-2010

我正在尝试使用FillDown将所有公式拉到某一行,跳过具有值的单元格...

如果我们看一下这个例子:

Example

在C列和E列中都有公式,在C6中我们用值15覆盖了单元格。

如果我们在第5行并且我希望代码将公式拖动到5行,则需要在C5中采用公式,并且因为C6有一个值,所以它会跳过这个,而是将它放入C7并拖动它下来。

有谁知道如何实现这个目标?

3 个答案:

答案 0 :(得分:2)

循环遍历列,如果单元格为空,则从第一个单元格复制公式?

喜欢这个吗?

Sub Sample()
    Dim rng As Range
    Dim lRow As Long, i As Long

    '~~> Change this to the relevant sheet name
    With ThisWorkbook.Sheets("Sheet1")
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> C2 Which I am assuming will have a formula
        Set rng = .Cells(2, 3)

        For i = 3 To lRow 
            If .Cells(i, 3).Value = "" Then rng.Copy .Cells(i, 3)
        Next i
    End With
End Sub

enter image description here

注意:对于您的情况,请根据需要进行修改。

修改

我强烈推荐@Captains Non VBA方式,如上所示。这是代码,以防有人正在寻找VBA选项。

Sub Sample()
    Dim rng As Range, blnkRng As Range
    Dim lRow As Long, i As Long

    '~~> Change this to the relevant sheet name
    With ThisWorkbook.Sheets("Sheet1")
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> C2 Which I am assuming will have a formula
        Set rng = .Cells(2, 3)

        On Error Resume Next
        Set blnkRng = .Range("C3:C" & lRow).SpecialCells(xlCellTypeBlanks)
        On Error GoTo 0

        If Not blnkRng Is Nothing Then rng.Copy blnkRng
    End With
End Sub

答案 1 :(得分:2)

如果您不想使用VBA路线,可以使用内置功能进行操作:

  1. 使用公式
  2. 复制单元格
  3. 突出显示目标范围,包括填充的单元格(例如整列)
  4. 在主页功能区上,在编辑,查找&选择打开"转到特殊"
  5. 选择"空白"在弹出窗口中单击确定
  6. 选择将仅更新为空白单元格,然后您可以粘贴
  7. 其他一些有用的东西" Go To Special"盒子也是!

答案 2 :(得分:0)

使用代码执行此操作(假设您要复制到第10行):

范围(" C3:C10")。SpecialCells(xlCellTypeFormulas).FormulaR1C1 =范围(" C2")。FormulaR1C1