Default AutoFill & xlCellTypeBlanks (VBA)

xldope

New Member
Joined
Aug 6, 2015
Messages
4
Please help!

I have a column in a worksheet that has data filled in sections with blank cells in between. I want to create a macro that will find a section of blank cells and perform a default autofill (xlFillDefault) based on the value in the cell above each blank section. Then repeat down to the end of the worksheet.

I've seen plenty of examples for filling blank cells with the cell above, but not for performing a default autofill.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this:
This is assuming we are dealing with Column 'A"
Code:
Sub Test_3()
Application.ScreenUpdating = False
Dim i As Integer
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        If Cells(i, 1).Value = "" Then
            Cells(i, 1).Value = Cells(i - 1, 1).Value
        End If
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That is probably because there isn't a default autofill what is your issue with filling blank cells with the cell above (edit: as per post #2)?
 
Upvote 0
Try this:
This is assuming we are dealing with Column 'A"
Code:
Sub Test_3()
Application.ScreenUpdating = False
Dim i As Integer
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To Lastrow
        If Cells(i, 1).Value = "" Then
            Cells(i, 1).Value = Cells(i - 1, 1).Value
        End If
    Next
Application.ScreenUpdating = True
End Sub

Sorry, the data is in column "B". The code updated blanks in column A which wasn't needed.
 
Upvote 0
This applies to Col A - change to suit. Is this what you want?
Code:
Sub DefaultAutoFill()
'Change column to suit
Dim Ar As Range, R As Range
Application.ScreenUpdating = False
With Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    On Error Resume Next
    Set R = .SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If R Is Nothing Then Exit Sub
    For Each Ar In R.Areas
        Ar(1).Offset(-1, 0).AutoFill Destination:=Ar(1).Offset(-1, 0).Resize(Ar.Cells.Count + 1, 1), Type:=xlFillDefault
    Next Ar
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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