使用上面的值填充任何空单元格

时间:2013-12-07 02:30:21

标签: excel excel-vba excel-formula vba

我想使用上面的单元格

填写所有空单元格
state  name
IL     Mike 
       Sam
CA     Kate
       Bill
       Leah

应如下

   state  name
    IL     Mike 
    IL     Sam
    CA     Kate
    CA     Bill
    CA     Leah

我尝试了以下

Sub split()
Dim columnValues  As Range, i As Long 

Set columnValues = Selection.Area

Set i = 1
For i = 1 To columnValues.Rows.Count   
    If (columnValues(i) = "") Then
    columnValues(i) = columnValues(i - 1)
    End If
Next

End Sub

设置i时出错。如何修改我的代码

6 个答案:

答案 0 :(得分:14)

对于那些不需要VBA的人,请选择ColumnA,Go To Special ...,Blanks和:

等于( = ),向上(), Ctrl + 输入

应该给出相同的结果。

答案 1 :(得分:6)

鉴于你要求VBA,有一种比循环更快的方法(VBA等同于上面提出的pnuts,以及最后删除公式的额外步骤):

On Error Resume Next
With Selection.SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With

答案 2 :(得分:3)

这是因为i应该定义为i=1。虽然代码有一些其他问题。我会把它改成这样的东西:

Sub split()
    Dim columnValues  As Range, i As Long

    Set columnValues = Selection

    For i = 1 To columnValues.Rows.Count
        If columnValues.Cells(i, 1).Value = "" Then
            columnValues.Cells(i, 1).Value = columnValues.Cells(i - 1, 1).Value
        End If
    Next
End Sub

答案 3 :(得分:0)

这是整个模块,我在最后粘贴了公式作为值。

Sub FillBlanksValueAbove()
Dim sName As String
sName = ActiveSheet.Name
Dim ws As Worksheet
Dim lastRow As Long, lastCol As Long
Dim rng As Range

'Set variable ws Active Sheet name
Set ws = Sheets(sName)

    With ws
    'Get the last row and last column
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    'Set the range
    Set rng = .Range(.Cells(1, 1), .Cells(lastRow, lastCol))
    rng.Select
    'Select Blanks
    rng.SpecialCells(xlCellTypeBlanks).Select
    'Fill Blanks with value above
    Selection.FormulaR1C1 = "=R[-1]C"
    'Paste Formulas as Values
    rng.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

    End With
End Sub

答案 4 :(得分:0)

 Sub fill_blanks()
 Dim i As Long
 i = 2 ' i<>1 because your first raw has headings "state " "name"
 'Assume state is in your cell A and name is in your cell B
 Do Until Range("B" & i) = ""
 Range("B" & i).Select
 If ActiveCell.FormulaR1C1 <> "" Then
    Range("A" & i).Select
      If ActiveCell.FormulaR1C1 = "" Then
           Range("A" & i - 1).Copy
           Range("A" & i).PasteSpecial Paste:=xlPasteValues
           Else
           i = i + 1
      End If
    Else
    i = i + 1
  End If
  Loop

结束子

答案 5 :(得分:0)

出于某种原因,帖子 https://stackoverflow.com/a/20439428/2684623 上使用的方法对我不起作用。执行 .value=.value 行时,我在单元格的值中收到错误“不可用”(本地语言为 #N/D)。 Office 版本为 365。

Error Fillin

我不知道原因,但经过一些修改后运行良好:

Sub TLD_FillinBlanks()
    
    On Error Resume Next
    With ActiveSheet.UsedRange.Columns(1)
        If .Rows(1) = "" Then .Rows(1).Value = "'"
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
End Sub

使用循环:

Sub TLD_FillinBlanksLoop()
    Dim rCell As Range
    
    For Each rCell In ActiveSheet.UsedRange.Columns(1).Cells
        If rCell.Value = "" And rCell.Row > 1 Then
            rCell.FillDown
        End If
    Next
    
End Sub

我希望这对某人有用。感谢和问候。