填充空白单元格(变体)

我有一个填充列的空白单元格的问题。

A,B,C有3个栏目标题

在那之下,我有可变数量的行,但列A和B将始终有数据。

C列可能有空隙。 我怎么可以做类似于:编辑>转到>特殊>空白,在公式栏中键入=,点击向上箭头,然后按Ctrl + Enter

除了macros只是直到A的最后一行而没有更进一步。

我有:

Sub FillCellsFromAbove() ' Turn off screen updating to improve performance Application.ScreenUpdating = False On Error Resume Next ' Look in column A With Columns(3) ' For blank cells, set them to equal the cell above .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" 'Convert the formula to a value .Value = .Value End With Err.Clear Application.ScreenUpdating = True End Sub 

然而,它从页面底部填充,而不是从最后一个“A”值所在的位置填充。

不要使用列C中的所有内容 – 首先确定列A中的数据延伸多远,然后抓取列C中的许多单元格:

 Sub FillCellsFromAbove() Dim R As Range, n As Long n = Range("A:A").Rows.Count n = Cells(n, "A").End(xlUp).Row Set R = Range(Cells(1, 3), Cells(n, 3)) Application.ScreenUpdating = False On Error Resume Next With R ' For blank cells, set them to equal the cell above .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" 'Convert the formula to a value .Value = .Value End With Err.Clear Application.ScreenUpdating = True End Sub 

在尝试将公式放入可能不存在的单元格之前,您可能需要testing空白。

 With Columns(3).Resize(Cells(Rows.Count, 1).End(xlUp).Row, 1) If CBool(Application.CountBlank(.Cells)) Then ' For blank cells, set them to equal the cell above .Cells.SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C" 'Convert the formula to a value .Value = .Value End If End With