如何在Excel中通过他们的下一个可用值填充空白单元格?

时间:2015-08-10 15:07:15

标签: excel excel-vba vba

我在Excel中有一些空白单元格,如图1所示。我想填充这些空白单元格,如图2所示。我的意思是,我想用下一个非空白值填充空白单元格。

我想用一个命令来做,因为我有大量的数据。任何人都可以帮我这样做吗?

2 个答案:

答案 0 :(得分:1)

生态学,

在MS Excel中,您可以使用“填充命令”自动填充相邻单元格中的数据。 请查看:https://support.office.com/en-za/article/Fill-data-automatically-in-worksheet-cells-74e31bdd-d993-45da-aa82-35a236c5b5db

您也可以使用VBA执行此操作。 http://www.extendoffice.com/documents/excel/771-excel-fill-blank-cells-with-value-above.html

答案 1 :(得分:1)

我有一个“常规”宏用于将数据复制到一列,我已经为你调整了,因为你想复制 up 。这是VBA的解决方案:

Sub GEN_USE_Copy_Data_Up_Column()
Dim screenRefresh As String, runAgain As String
Dim lastRow As Long, newLastRow As Long
Dim CopyFrom As Range
Dim LastRowCounter As String

screenRefresh = MsgBox("Turn OFF screen updating while macro runs?", vbYesNo)
If screenRefresh = vbYes Then
Application.ScreenUpdating = False
Else
Application.ScreenUpdating = True
End If

Dim EffectiveDateCol As Integer
LastRowCounter = InputBox("What column has the most data (this info will be used to find the last used row). Use Letters")

CopyAgain:
With ActiveSheet
    'lastRow = .Cells(.Rows.Count, LastRowCounter).End(xlUp).row
    lastRow = .UsedRange.Rows.Count
End With


' THIS WILL ASK THE USER TO SELECT THE COLUMN TO COPY DATA DOWN
MsgBox ("Now, you will choose a column, and that column's data will be pasted in the range below the current cell, to the next full cell")
Dim Column2Copy As String
Column2Copy = InputBox("What column (A,B,C, etc.) would you like to copy the data of?")

Dim startCell As Range
Set startCell = Cells(1048576, Column2Copy).End(xlUp)

'Cells(1, Column2Copy).End(xlDown).Select
Do While startCell.Row > 1
    If startCell.End(xlUp).Row = 1 Then
        newLastRow = 1
        Else
        newLastRow = startCell.End(xlUp).Offset(1, 0).Row
    End If
    Set CopyFrom = startCell
    If startCell.Row Mod 5 = 0 Then Debug.Print startCell.Row
    Range(Cells(startCell.Row, Column2Copy), Cells(newLastRow, Column2Copy)).Value = CopyFrom.Value
    Set startCell = startCell.End(xlUp)
    'startCell.Select
Loop

runAgain = MsgBox("Would you like to run the macro on another column?", vbYesNo)
If runAgain = vbNo Then
Cells(1, 1).Select
If screenRefresh = vbYes Then
Application.ScreenUpdating = True
Else
Application.ScreenUpdating = True
End If

Exit Sub

ElseIf runAgain = vbYes Then
GoTo CopyAgain
End If

End Sub

由于它是一个“常规用途”宏,我会提示您询问要复制哪一列以及是否要更新屏幕。如果您不需要这些,请告诉我,我可以删除/删除一些“绒毛”并留下宏的主要部分。