Excel Auto fill down VBA macro

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
204
Office Version
  1. 2019
Platform
  1. Windows
In my Excel sheet there are ABCDE columns

D column is filled in with data. & I am filling down BCE columns by selecting & Pressing CTRL+D. I want a VBA macro to do this, the sheet is of 1000 rows.

excel%2Bfill%2Bdown%2Bbefore.JPG


data looks like this

excel%2Bfill%2Bdown%2Bafter.JPG

I want the data to be like this
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Sub Macro1()

Dim x As Long
Dim y As Long
Dim lastrow As Long
Dim lastcolumn As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False

lastcolumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count

For x = 2 To lastrow

If Cells(x, 2).Value = "" Then
Cells(x, 2).Value = Cells(x - 1, 2).Value
Cells(x, 3).Value = Cells(x - 1, 3).Value
Cells(x, 5).Value = Cells(x - 1, 5).Value
End If

Next x
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Sub Macro1()

Dim x As Long
Dim y As Long
Dim lastrow As Long
Dim lastcolumn As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False

lastcolumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count

For x = 2 To lastrow

If Cells(x, 2).Value = "" Then
Cells(x, 2).Value = Cells(x - 1, 2).Value
Cells(x, 3).Value = Cells(x - 1, 3).Value
Cells(x, 5).Value = Cells(x - 1, 5).Value
End If

Next x
Application.ScreenUpdating = True

End Sub


How can I select range with this code ? and how to define the last row, the last row is being copying for so many rows
 
Upvote 0
select the sheet and run the code, keep your data from row two on wards, assuming you have headers in row 1.
 
Upvote 0
The following macro is based upon my previous two posts.
Assuming Column D can be used to find the last row (and there are no fomulas in col D):
Code:
Sub FillBlanks()
Dim rng As Range
On Error Resume Next
Set rng = Range([A1], Cells(Cells(Rows.Count, "D").End(xlUp).Row, "E"))
Application.DisplayAlerts = False
With rng
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
End With
Application.DisplayAlerts = True
On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,185
Members
448,872
Latest member
lcaw

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