Solved

Fill adjacent column in excel with data from previous column

Posted on 2013-05-17
2
235 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
[X]
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
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

705 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