VBA to fill the empty cells above from the value below.

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Please could someone help me out?

I am trying to get a VBA code that fills all the empty cells above in column G with the same value that is below.

So lets say 'DB' is in cell G20 and 'AL' is in cell G10.

I would like the code to fill cells G19:G11 with 'DB'

Any help would be much appreciated.

Thanks

Dan
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Assuming ..
- the blank cells are truly blank
- column G does not contain any formulas that need to be retained
.. try
Code:
Sub FillBlanks()
  With Range("G1", Range("G" & Rows.Count).End(xlUp))
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[1]C"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Hi Peter,

Your code works perfectly, Thank you.

Is it possible to widen your code to do exactly the same in column H and I?

Thanks again

Dan
 
Upvote 0
Is it possible to widen your code to do exactly the same in column H and I?
It is, but the best method of doing so depends on the possible structure of your data, which we don't know about.
Does this do what you want?
If not, tell us more about those 3 columns. In particular, whether they all finish on the same row or not.
Rich (BB code):
With Range("G1", Range("I" & Rows.Count).End(xlUp))
 
Upvote 0
Hi Peter,

Your code does exactly what I was wanting. Thank you again for all your help.

Kind Regards

Dan
 
Upvote 0
Hi Peter,

This code you did for me yesterday is now coming up with an 'Empty Cell Error'.

Code:
Sub FillBlanks()With Range("G3", Range("I" & Rows.Count).End(xlUp))
.SpecialCells(xlBlanks).FormulaR1C1 = "=R[1]C"
    .Value = .Value
  End With
End Sub

I've altered the header and that's why I am getting the error.

The reason it was ok before was because I had merged the header meaning cell G1 was empty.

I need it to run from G3 now and as you can see I've change the code from G1 to G3 hoping that would sort it but it hasn't.

Is there an easy fix?

Thanks again

Dan
 
Upvote 0
Can you give us a small set of dummy data for which the code fails?

If there are still any merged cells involved in the sample please make clear which one(s).

What is the exact error message?
 
Upvote 0
Hi,

The error is: Run-time error '1004': No cells were found.

and when I debug, it highlights this line
Code:
.SpecialCells(xlBlanks).FormulaR1C1 = "=R[1]C"

The merged cells was A1:J1 and that had the sheet header in.

Now there isn't any merged cells and the data I will be entering onto starts on row 3.

Thanks again

Dan
 
Upvote 0

Forum statistics

Threads
1,214,858
Messages
6,121,960
Members
449,057
Latest member
FreeCricketId

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