Filling out blank spaces with previous value

edTech

New Member
Joined
Dec 8, 2019
Messages
33
Office Version
  1. 2019
Platform
  1. Windows
I have data in an excel sheet that has blank spaces in column B and C. I want to fill them with its previous values. I have the VbScript code below semi working as I think my issue is the FormulaR1C1, but I cannot get it to work. Any suggestions?

Sample Data:
A​
B​
C​
TimeStart RaceEnd Race
225586.586.5
263498.5
578988.9

Code:

With objReadWS

lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

Set rng = .Range(.Cells(2, 2), .Cells(lastRow, lastCol))
rng.Select

rng.SpecialCells(xlCellTypeBlanks).Select

rng.FormulaR1C1 = "=R[-1]C[2]"

rng.Select
rng.Copy
rng.PasteSpecial -4163, -4142, True, False


End With
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
VBA Code:
   With objReadWS
      lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
      lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
      
     With .Range(.Cells(2, 2), .Cells(lastrow, lastCol))
         .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
         .Value = .Value
      End With
   End With
 
Upvote 0
Or without formulas
Code:
Sub t()
Dim c As Range
For Each c In Range("B2", Cells(Rows.Count, 1).End(xlUp).Offset(, 2))
    If c = "" Then c = c.Offset(-1).Value
Next
End Sub
 
Upvote 0
Thank you Fluff and JLGWhiz both methods worked fantastic.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Glad we could help & thanks for the feedback

Your welcome, if I wanted to stop at column C instead of having it go to the last empty column how would I change this line? lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
 
Upvote 0
Do you mean you only want it to work on columns B & C?
 
Upvote 0
How about
VBA Code:
   With objReadWS
      lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
      lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
      
     With .Range("B2:C" & lastrow)
         .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
         .Value = .Value
      End With
   End With
 
Upvote 0
How about
VBA Code:
   With objReadWS
      lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
      lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
     
     With .Range("B2:C" & lastrow)
         .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
         .Value = .Value
      End With
   End With

Perfect! that was awesome ... I was struggling all night with that
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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