?
Solved

Fill adjacent column in excel with data from previous column

Posted on 2013-05-17
2
Medium Priority
?
236 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 2000 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 93

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

777 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