Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Fill adjacent column in excel with data from previous column

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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 …

839 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