Solved

Fill adjacent column in excel with data from previous column

Posted on 2013-05-17
2
192 Views
Last Modified: 2013-10-23
I have the following vba code.  It is not working 100% correctly.  I have several columns of data and I would like this code to analyze the cell and the adjacent cell.  If the adjacent cell is blank, use the previous cell data.  If the adjacent call is not blank, store that data and look at the next adjacent cell and if blank, use previous cell data to populate.  What this code is doing is just looking one cell and using it to fill in all of the rest, no matter if they are not blank.


Open in new window

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(2, 3), .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
0
Comment
Question by:Fairfield
2 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
SpecialCells has a limitation of 8192 non-contiguous areas in Excel 2007 and earlier. If it needs to return more than 8192 non-contiguous areas, it returns all the cells instead.

If you are processing a large amount of data in Excel 2007 and earlier, then you need to break the range up into chunks of say 16,000 cells at a time. That way, you can never exceed the 8192 non-contiguous areas from SpecialCells.
Sub FillRight()
Dim rg As Range, rg2 As Range, rgg As Range
Dim i As Long, n As Long, nCols As Long, nRows As Long
With ActiveSheet
    Set rg = Selection.Cells
    If rg.Cells.Count = 1 Then Set rg = .UsedRange
    Set rgg = Range(.Cells(2, 3), .Cells(.Rows.Count, .Columns.Count))
    Set rg = Intersect(rg, .UsedRange)
    Set rg = Intersect(rg, rgg)
End With
nCols = rg.Columns.Count
nRows = rg.Rows.Count
'The SpecialCells method has a limitation of 8192 non-contiguous areas in Excel 2007 and earlier.
n = Int(16000 / nCols)      'Maximum number of rows to process at a time
For i = 1 To nRows Step n
    Set rg2 = rg.Cells(i, 1).Resize(n, nCols)
    Set rg2 = Intersect(rg, rg2)
    On Error Resume Next
    Set rg2 = rg2.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If Not rg2 Is Nothing Then
        rg2.FormulaR1C1 = "=RC[-1]"
    End If
Next
End Sub

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Brad wrote:

SpecialCells has a limitation of 8192 non-contiguous areas in Excel 2007 and earlier. If it needs to return more than 8192 non-contiguous areas, it returns all the cells instead.

Quite true.  Another way to handle it is to first sort the data to ensure that the range you need returned via SpecialCells is one contiguous block.

If you need to retain the original order of the data, then you can have the code use a "dummy" column to indicate the original ordering, do the sort, use Special Cells, re-sort to get the original order, and then clear out the dummy column.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now