Solved

Fill adjacent column in excel with data from previous column

Posted on 2013-05-17
2
227 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 81

Accepted Solution

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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.

733 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