If cell is blank the copy the value above it.

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,338
Office Version
  1. 365
Platform
  1. Windows
I have a table named "CMCS_BoM_MakeTable" In that table I have a Field/Column named "WBS"

My tab name is Material Ad Hoc MT

I need to go down the WBS column and if there is a cell I need to populate it with the value that's directly above it (the row above it). I want to use Column A (or "Task ID") to determine the Last Row.

Is this possible in VBA?

THANK YOU FOR YOUR TIME AND HELP.

PS there could be two consecutive cells that are blank - So I need the code to run from the top to the bottom so that it fills the first blank and then fills the next consecutive blank with the value that was filled with the one above it - so that I do not have any blanks in that row. Thank you!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I am very proud of myself - lol

Code:
Sub FillBlankCells()

Dim LastRow As Long
Dim Cnt As Long
Dim Cnt1 As Long

Sheets("Material Ad Hoc MT").Select

LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For Cnt = 1 To LastRow

Cnt1 = Cnt - 1

        If Cells(Cnt, 2).Value = "" Then
        Cells(Cnt, 2).Value = Cells(Cnt1, 2)
        End If
        
    Next Cnt

End Sub

But if someone sees a problem with this code or can show me a better way, please let me know.
 
Upvote 0
This:
VBA Code:
For Cnt = 1 To LastRow

Cnt1 = Cnt - 1

        If Cells(Cnt, 2).Value = "" Then
        Cells(Cnt, 2).Value = Cells(Cnt1, 2)
        End If
        
    Next Cnt
could all be simplified to just this:
VBA Code:
For Cnt = 1 To LastRow
    If Cells(Cnt, 2).Value = "" Then Cells(Cnt, 2).Value = Cells(Cnt-1, 2)
Next Cnt
 
Upvote 0
Solution
Nothing wrong with the code you've posted.:)

You could do it without looping though.
VBA Code:
Sub FillBlanks()
Dim tbl As ListObject

    Set tbl = Sheets("Material Ad Hoc MT").ListObjects("CMCS_BoM_MakeTable")

    With tbl.ListColumns("WBS").Range
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With

End Sub
 
Upvote 0
Hi Norie. That would need error handling using special cells in case there are none.
 
Upvote 0
Thank you all. This is very educational.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

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