Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling any empty cells with the value above

I want to fill in all empty cells using values of above cells

state  name
IL     Mike 
       Sam
CA     Kate
       Bill
       Leah

Should be as follows

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

I tried the following

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 get an error when I set i. How can I modify my code

like image 226
Aleksei Nikolaevich Avatar asked Dec 07 '13 02:12

Aleksei Nikolaevich


People also ask

How do you fill blank cells with value above in pivot table?

You need to click in your Pivot Table > PivotTable Analyze > Options > Format > For empty cells show: enter a value or text in this box. This is how you can replace pivot table blank cells with 0!

How do you fill blank cells with value above Ctrl G in Excel?

Re: Excel - how to fill blank cells with value from above Go back to the situation where the cells are blank. Select columns A and B. Select General as number format. Press F5 or Ctrl+G.


2 Answers

For those not requiring VBA for this, select ColumnA, Go To Special..., Blanks and:

Equals (=), Up (), Ctrl+Enter

should give the same result.

like image 71
pnuts Avatar answered Sep 24 '22 13:09

pnuts


Given you asked for VBA, there is a quicker way than looping (the VBA equivalent of what pnuts posed above, with the additional step of removing the formula at the end):

On Error Resume Next
With Selection.SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
like image 34
brettdj Avatar answered Sep 23 '22 13:09

brettdj