Fill in empty cells with adjacent cell value

dystophic

New Member
Joined
May 31, 2017
Messages
3
Good afternoon,

I was wondering if someone can help -

I have a column E containing cells of different values; some of those cells are blank. I also have an adjacent column D that doesn't have any blank values.

What I basically trying to do is use VBA fill in the empty cells in column E with the value of their adjacent column D cells. As below:

Column DColumn E
ZJ745433

<tbody>
</tbody>
19180597/1

<tbody>
</tbody>
ZJ746529

<tbody>
</tbody>
ZJ745000

<tbody>
</tbody>
19178853/1

<tbody>
</tbody>
ZJ750759

<tbody>
</tbody>

<tbody>
</tbody>
to
Column DColumn E
ZJ745433

<tbody>
</tbody>
19180597/1
ZJ746529

<tbody>
</tbody>
ZJ746529
ZJ745000

<tbody>
</tbody>
9178853/1
ZJ750759

<tbody>
</tbody>
ZJ750759

<tbody>
</tbody>











Thank-you.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I would create a new column with a formula in each cell.

=if(E2="",D2,E2)

Hide column E. Use the new column instead.
 
Upvote 0
Last edited:
Upvote 0
Thank you for your suggestions!

Apologies, I haven't been very clear with my request. I need to do this specifically with the use of a macro in VBA.

Also sorry for the bad table formatting in the first post!
 
Upvote 0
One way is to turn on your Macro Recorder and perform the steps I pointed you to.

Here is a cleaned up version of that with comments added:
Code:
    Dim lastRow As Long

'   Find last row in column D with data
    lastRow = Cells(Rows.Count, "D").End(xlUp).Row
    
'   Populate all blank cells in column E with formula
    Range("E1:E" & lastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=RC[-1]"
 
Last edited:
Upvote 0
Code:
Sub CopyAdjacent()    
    Dim sh As Worksheet
    Dim rowCount As Long
    
    'Insert the worksheet name
    Set sh = ThisWorkbook.Sheets("Sheet1")
    
    'Count the row having data in the column D
    
    rowCount = sh.Range("D1", sh.Range("D1").End(xlDown)).Rows.Count


    For i = 2 To rowCount
    
    ' Fill empty cells of the column E with the adjacent cell value
    If IsEmpty(Cells(i, 5).Value) Then
        Cells(i, 5).Value = Cells(i, 4).Value
    End If
    
    Next i


End Sub
 
Upvote 0
Welcome to the Board Frank!

Loops will certainly work, but whenever possible, it is best to avoid using them. They tend to be a bit slow and memory hogs. For large files, the time lag can become quite pronounced.
Sometimes there isn't really another good option and you have to use loops (I do it all the time), but when there is, it is usually better to go the other route.
 
Upvote 0
That worked perfectly!

Really happy with the response time and solutions from all of you! Thanks a lot.
 
Upvote 0
You are welcome!

One last tip. If you want to change the formulas used to fill those blank cells with hard-coded values instead, just add this line to the end of the code I posted above:
Code:
Range("E1:E" & lastRow).Value = Range("E1:E" & lastRow).Value
 
Upvote 0
Welcome to the Board Frank!
Thank you Joe4!!

Loops will certainly work, but whenever possible, it is best to avoid using them. They tend to be a bit slow and memory hogs. For large files, the time lag can become quite pronounced.
Sometimes there isn't really another good option and you have to use loops (I do it all the time), but when there is, it is usually better to go the other route.
Thanks for the clarification and for the suggestion!!! I will apply your tip in the future.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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