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:

[TABLE="width: 500, align: left"]
<tbody>[TR]
[TD]Column D[/TD]
[TD]Column E[/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ745433[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]19180597/1[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ746529[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ745000[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]19178853/1[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ750759[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
to [TABLE="width: 500, align: right"]
<tbody>[TR]
[TD]Column D[/TD]
[TD]Column E[/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ745433[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]19180597/1[/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ746529[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]ZJ746529[/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ745000[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]9178853/1[/TD]
[/TR]
[TR]
[TD][TABLE="width: 146"]
<tbody>[TR]
[TD]ZJ750759[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD]ZJ750759[/TD]
[/TR]
</tbody>[/TABLE]











Thank-you.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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
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,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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