• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Excel VBA for filling adjacent cells

I need some code to fill adjacent cells with values from a preceding cells.  However, if the cell contains a value alread, I need it to skip the cell.


I woud like the values form the adjacent (column) cells updated by the one from the preceding cell.  However, if there is a value in the cells to be updated, I want to use it as the new value and fill in the remaining adjacent cells.
excel.JPG
0
Fairfield
Asked:
Fairfield
1 Solution
 
chrisezardCommented:
Do you need this to be automated by VBA?
If you just need to make a few infills, then...
Highlight the value you want to copy and the empty cells to the right of it that you want to fill, and press Ctrl+R. The empty cells will have the same value as the first cell you selected.
0
 
FairfieldAuthor Commented:
I would like this to be automated with VBA as there are over 38k rows of data.
0
 
byundtCommented:
Here is a macro to fill values to the right. As written, it works with the used range, starting with column B.
Sub FillRight()
Dim rg As Range, rgg As Range
With ActiveSheet
    Set rg = Selection.Cells
    If rg.Cells.Count = 1 Then Set rg = .UsedRange
    Set rgg = Range(.Cells(1, 2), .Cells(.Rows.Count, .Columns.Count))
    Set rg = Intersect(rg, .UsedRange)
    Set rg = Intersect(rg, rgg)
End With
On Error Resume Next
Set rg = rg.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not rg Is Nothing Then
    rg.FormulaR1C1 = "=RC[-1]"
End If
End Sub

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now