Solved

Excel VBA for filling adjacent cells

Posted on 2013-05-15
3
313 Views
Last Modified: 2013-05-17
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
Comment
Question by:Fairfield
3 Comments
 
LVL 1

Expert Comment

by:chrisezard
ID: 39169263
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
 

Author Comment

by:Fairfield
ID: 39169537
I would like this to be automated with VBA as there are over 38k rows of data.
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39169954
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Frequency distribution of horizontal data? 5 30
first name and last initial in excel 11 29
A Table within a User Form 24 44
Loop with matrices. Please help me! 16 42
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question