简体   繁体   中英

Excel VBA - Fill in empty database cells with variable range formula

I have a database containing "N/A" at different cells, caused by temporary system offline. I used to fill these "N/A"'s using the formula shown with the image:

As you can see, the datum at columns A are cumulative, so are B and C. The "N/A"s are compensated by spreading the difference between the last reading before the first "N/A" and the first reading after the last "N/A"

The "N/A"'s appear at different points and have varying lengths.

Is there maybe a vba code to help me do this in one click for the entire database? Thank you sirs for your kind help.

You could try running this VBA code to interpolate the N/A values based on numbers above and below:

Sub FillNA()

Dim rng As Range, col As Range, a As Range
Set rng = Cells.SpecialCells(xlCellTypeConstants, xlTextValues)

For Each col In ActiveSheet.UsedRange.Columns    
    If Not Intersect(rng, col) Is Nothing Then
        For Each a In Intersect(rng, col).Areas
            If a(1) = "N/A" Then
                a(0).Resize(a.Count + 2).DataSeries Trend:=True
            End If
        Next a    
    End If
Next col

End Sub

(If N/A appears at the beginning or end of the range you will need to decide how to handle this case and make appropriate adjustments)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM