Fill cells with the contents of the next non-blank cell above

Status
Not open for further replies.

ellison

Active Member
Joined
Aug 1, 2012
Messages
356
Office Version
  1. 365
Platform
  1. Windows
Hi, trying to (repeatedly!) fill cells with the contents of the next non-blank cell above.
And we can see how to go through the standard procedure of highlighting the cells, then: Home / Find & Select / Go To Special / Fill in Blanks / Enter Formula / Hit Ctrl Enter
But between fat fingers and having to repeat this over and over, we are seeing some mistakes.
Is there some code or another way of doing this to give us the result we are trying to aim at? :unsure:

Hopefully these 2 mini sheets show V1 with blanks cells throughout. And V2 showing how we would like it to end up...


fill-from-next-none-blank-cell-above-question.xlsm
AB
1RowV1
22
331
44fill
55
66
77in
88
99
1010
1111
1212the
1313blanks
1414
1515
Sheet1



fill-from-next-none-blank-cell-above-question.xlsm
AB
1RowV2 with blanks filled in
22
331
44fill
55fill
66fill
77in
88in
99in
1010in
1111in
1212the
1313blanks
1414blanks
1515blanks
Sheet2



Thanks for taking a look!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Here's one way (run while the sheet is active)
VBA Code:
Sub FillMeUp()
    Dim c As Range
    For Each c In Range("B3:B" & Cells(Rows.Count, "A").End(xlUp).Row)
        If c = "" Then c.FillDown
    Next c
End Sub
 
Upvote 0
Actually Kevin, really sorry - I've just plugged this onto a sheet with a 4K rows and it's gone really slow...
Literally nothing else on the sheet apart from the 2 columns of data.
I was just wondering if there may be a tweak that can be done?!
 
Upvote 0
Tested the following on 14K rows of data based on your OP. Took 0.06 secs. Change the sheet name to suit.

VBA Code:
Option Explicit
Sub FasterFill()
    Dim ws As Worksheet, a, b, i As Long, s As String
    Set ws = Worksheets("Sheet1")                   '<-- *** Change to actual sheet name ***
    a = ws.Range("B3:B" & ws.Cells(Rows.Count, "A").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        If a(i, 1) <> "" Then
            s = a(i, 1)
            b(i, 1) = s
        Else
            b(i, 1) = s
        End If
    Next i
    ws.Range("B3").Resize(UBound(b, 1), 1).Value = b
End Sub
 
Upvote 0
Actually, I could have used slightly less code:
VBA Code:
Sub FasterFill_V2()
    Dim ws As Worksheet, a, i As Long, s As String
    Set ws = Worksheets("Sheet1")                   '<-- *** Change to actual sheet name ***
    a = ws.Range("B3:B" & ws.Cells(Rows.Count, "A").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        If a(i, 1) <> "" Then s = a(i, 1)
        b(i, 1) = s
    Next i
    ws.Range("B3").Resize(UBound(b, 1), 1).Value = b
End Sub
 
Upvote 0
Solution
what happend when depend of 2 values mi case:
actually:
1702444261061.png

Expected result:
1702444279451.png


the rows 11 and 12 not was filled because the combination initially dont have any value in column C

Thanks
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top