+ Reply to Thread
Results 1 to 3 of 3

Horizontal interpolation for >1 empty cell

Hybrid View

  1. #1
    Registered User
    Join Date
    01-29-2016
    Location
    Melbourne
    MS-Off Ver
    2010
    Posts
    1

    Horizontal interpolation for >1 empty cell

    Hi

    I'm a bit of an excel noob but getting better as time goes on.

    I have a large spreadsheet and need to fill missing values with the average of the nearest neighbouring cell that has a value.

    For example, some blank cells will have a neighbouring data point on each side, but some might have 2 or 3+ blank cells in a row before touching a cell with a datapoint. I want to fill all these cells just with a calculation that is also dependent on the number of cells blank, so it would need to change with the amount of blank cells.

    eg
    [A] [(A+B)/2] [B]
    [A] [A + (B-A)(1/3)] [A + ((B-A)(2/3)] [B]


    Thanks, any help is appreciated. I'll continue googling in the mean time.

    Edit: It appears a stepwise fill command in excel can do this but only when I select the row manually. Can I apply this to the whole sheet?

    Edit: It further appears that I need to do a linear interpolation for all missing data points, which can automatically find the closest horizontal data points and apply linear interpolation to the blanks in between, without modifying the existing data points. The biggest challenge seems to be that the guides on the internet use a strict reference point whereas I need a conditional reference which searches for the nearest data point from any given empty cell.
    Last edited by mb1z; 01-29-2016 at 09:41 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Horizontal interpolation for >1 empty cell

    I have created the attached user defined function to interploate in columns

    you might enjoy seeing if you can modify it to work on rows.

    Public MyArray As Variant
    
    Function Interpolate(Myrange As Range)
    Interpolate = ""
    If InStr(Myrange.Address, ":") = 0 Then GoTo skip2
    Rangein = Split(Replace(Myrange.Address, ":", ""), "$")
    If Rangein(2) = Rangein(4) Then GoTo skip2
    If Myrange.Row = Application.Caller.Row Then
    MyArray = Application.Transpose(Myrange.Value)
    
    FillLoop:
    For Pos = 1 To UBound(MyArray)
    If MyArray(Pos) = "" Then Exit For
    Next
    
    If Pos >= UBound(MyArray) Then GoTo Skip1
    
    For Pos2 = Pos + 1 To UBound(MyArray)
    If MyArray(Pos2) <> "" Then Exit For
    Next
    
    If Pos2 = UBound(MyArray) And MyArray(Pos2) = "" Then GoTo Skip1
    
    Variation = (MyArray(Pos2) - MyArray(Pos - 1)) / (Pos2 - Pos + 1)
    
    For Fill = Pos To Pos2 - 1
    MyArray(Fill) = MyArray(Fill - 1) + Variation
    Next
    
    GoTo FillLoop
    
    Skip1:
    Interpolate = MyArray(1)
    Else
    Interpolate = MyArray(Application.Caller.Row - Myrange.Row + 1)
    End If
    skip2:
    'Exit Function
    End Function
    Attached Files Attached Files
    Last edited by mehmetcik; 01-29-2016 at 11:56 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Horizontal interpolation for >1 empty cell

    
    Public MyArray As Variant
    
    Function HInterpolate(Myrange As Range)
    HInterpolate = ""
    If InStr(Myrange.Address, ":") = 0 Then GoTo skip2
    Rangein = Split(Replace(Myrange.Address, ":", ""), "$")
    If Rangein(1) = Rangein(3) Then GoTo skip2
    If Myrange.Column = Application.Caller.Column Then
    MyArray = Application.Transpose(Application.Transpose(Myrange.Value))
    
    FillLoop:
    For Pos = 1 To UBound(MyArray)
    If MyArray(Pos) = "" Then Exit For
    Next
    
    If Pos >= UBound(MyArray) Then GoTo Skip1
    
    For Pos2 = Pos + 1 To UBound(MyArray)
    If MyArray(Pos2) <> "" Then Exit For
    Next
    
    If Pos2 = UBound(MyArray) And MyArray(Pos2) = "" Then GoTo Skip1
    
    Variation = (MyArray(Pos2) - MyArray(Pos - 1)) / (Pos2 - Pos + 1)
    
    For Fill = Pos To Pos2 - 1
    MyArray(Fill) = MyArray(Fill - 1) + Variation
    Next
    
    GoTo FillLoop
    
    Skip1:
    HInterpolate = MyArray(1)
    Else
    HInterpolate = MyArray(Application.Caller.Column - Myrange.Column + 1)
    End If
    skip2:
    'Exit Function
    End Function
    
    Function Interpolate(Myrange As Range)
    Interpolate = ""
    If InStr(Myrange.Address, ":") = 0 Then GoTo skip2
    Rangein = Split(Replace(Myrange.Address, ":", ""), "$")
    If Rangein(2) = Rangein(4) Then GoTo skip2
    If Myrange.Row = Application.Caller.Row Then
    MyArray = Application.Transpose(Myrange.Value)
    
    FillLoop:
    For Pos = 1 To UBound(MyArray)
    If MyArray(Pos) = "" Then Exit For
    Next
    
    If Pos >= UBound(MyArray) Then GoTo Skip1
    
    For Pos2 = Pos + 1 To UBound(MyArray)
    If MyArray(Pos2) <> "" Then Exit For
    Next
    
    If Pos2 = UBound(MyArray) And MyArray(Pos2) = "" Then GoTo Skip1
    
    Variation = (MyArray(Pos2) - MyArray(Pos - 1)) / (Pos2 - Pos + 1)
    
    For Fill = Pos To Pos2 - 1
    MyArray(Fill) = MyArray(Fill - 1) + Variation
    Next
    
    GoTo FillLoop
    
    Skip1:
    Interpolate = MyArray(1)
    Else
    Interpolate = MyArray(Application.Caller.Row)
    End If
    skip2:
    'Exit Function
    End Function
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Paste formula to the last empty cell based on a non-empty cell on another column
    By bhenlee in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-02-2015, 04:07 PM
  2. Replies: 4
    Last Post: 06-07-2013, 03:09 AM
  3. Copy cells from Vertical to Horizontal (with empty cells in between)
    By epistimon in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 12-11-2012, 02:54 AM
  4. Ignore empty cells when doing horizontal sort with VBA
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2012, 09:01 PM
  5. Order horizontal lines by skipping empty cells
    By Hitch75 in forum Excel General
    Replies: 4
    Last Post: 09-07-2010, 05:24 PM
  6. Replies: 0
    Last Post: 07-24-2008, 02:27 PM
  7. help with interpolation and limit of interpolation
    By uriel78 in forum Excel General
    Replies: 0
    Last Post: 02-18-2005, 10:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1