Solved

Excel vba to fill in blank cells with previous cell value

Posted on 2014-09-23
5
3,618 Views
Last Modified: 2014-09-24
I have a large transaction file, 250,000 lines, and therre are approximately 39,000 where the date cells are blank.  I want to be able to loop through the file and replace a blank cell with the value in the cell above.

Here is a small sample of the data:

Transaction Date    Acct Code   TxnCode
4/13/2010      00006350       84443
4/13/2010      00006350      36415
4/13/2010      00006350      43239
                     00006350                      43239
                     00006358                      85025
                     00006358                      36415
4/14/2010      00005143       19120
4/14/2010      00005270       81025


What would be the vba code to loop through these lines and replace the transaction date values?

Thanks

Glen
0
Comment
Question by:GPSPOW
5 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40340414
Assuming the date is in column 1

Sub FillIn()

Dim strlast As String
Dim lngRow As Long

strlast = Range("A2")

For lngRow = 2 To ActiveSheet.UsedRange.Rows.Count
    If Cells(lngRow, 1).Text = "" Then
        Cells(lngRow, 1).Value = strlast
    Else
        strlast = Cells(lngRow, 1).Text
    End If
Next

End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40340418
Here is the code you can use :
Sub check_n_fill
Dim range As range, cell As range, value As String
Set range = range("A2:A9")
For Each cell In range
If Trim(cell.value) <> "" Then
value = cell.value
Else
cell.value = value
End If
Next cell
End Sub

Open in new window

0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 40340431
Hi Glen,

Try this routine. Adjust the Range specified to suit your needs.

Sub FillBlanks()

    Dim rng As Range
    Set rng = Range("A2:A33") 'the range containing the blank cells that you wish to fill
    
    With rng.SpecialCells(xlCellTypeBlanks)
        .FormulaR1C1 = "=R[-1]C"
        .Value = .Value
    End With
    
End Sub

Open in new window


Regards,

Wayne
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 40341161
You don't have to use VBA unless you particularly want to. Downside of using VBA is that you lose your Undo history.

Apply an AutoFilter to the data and set the filter on column A such that it shows only blanks. In the first blank cell type a formula linking it to the previous row, eg if it is row 4 type =A3

Select this cell and the remainder of the blank cells in column A as a block and press Ctrl + D to fill down. This will only populate the visible cells leaving the hidden/filtered cells with theie existing values.

Remove the filter and copy/paste values if so required.

Thanks
Rob H
0
 

Author Closing Comment

by:GPSPOW
ID: 40342016
This worked perfectly.

Took 2 seconds.

Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
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 …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now