Auto filling cells with no data using VBA

forensic93

New Member
Joined
Jan 14, 2020
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I was wondering if it was possible to back fill data using a macro?
I have one column where every 4th cell there is data, and one column where every 16th cell there is data.

Essentially i want to know if i can get a macro to read the 4th cell and if the 5th cell has nothing then write what the 4th cell has in it. But if there is data, then that becomes the new number used to fill in empty cells if that makes sense.

Is this possible to do? I have added my attempted code below.

VBA Code:
Sub fill_cell() 
    Dim rw As Long 
    Dim Sheet As interger
    Sheet = 1
    With Sheets("Sheet") 
for rw = 1 To Cells(Rows.Count, "B").End(xlUp).Row 
            If Cells(rw, "E") = "" Then Cells(rw, "E") = Cells(rw, "E4")
        Next rw 
    End With 
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Check the image below

I hope this works :)

VBA Code:
Sub fill_cell()
    Dim rw As Integer
    
    With Sheets("Sheet")
        For rw = 1 To .Range("A500000").End(xlUp).Row
            If .Range("A" & rw).Value = "" Then
               .Range("A" & rw).Value = .Range("A" & rw - 1).Value
            End If
        Next rw
    End With
End Sub
 

Attachments

  • image_1.png
    image_1.png
    6.6 KB · Views: 7
Upvote 0
Try
VBA Code:
Sub fill_cell()
    Dim rw As Long
    With Sheets("Sheet")
    For rw = 1 To Cells(Rows.Count, "B").End(xlUp).Row
    If .Range("A" & rw).Value = "" Then
               .Range("A" & rw).Value = .Range("A" & rw - 1).Value
            End If
        Next rw
    End With
End Sub
 
Upvote 0
Don't know how big your actual data is, but you can do them all at once instead of cycling through a row at a time.

VBA Code:
Sub fillblanks()
  With Range("A1", Range("B" & Rows.Count).End(xlUp).Offset(, -1))
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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