Consulting

Results 1 to 6 of 6

Thread: Linear Interpolation

  1. #1
    VBAX Regular
    Joined
    Dec 2016
    Posts
    22
    Location

    Linear Interpolation

    Hi all,

    I have a column of data with a set of known values that are populated in descending order. Between these known values are several blank cells. The number of blank cells between known values varies considerably.

    I'm trying to develop a macro that will populate the intermediate values between the known values (just through linear interpolation) into those blank cells.

    E.g.

    If I have '1, blank cell, blank cell, blank cell, 2, blank cell, blank cell, 6'

    Then the macro should produce:

    '1, 1.25, 1.50, 1.75, 2, 3.33, 4.67, 6'

    Attached is an example of the dataset I have (Column A), and what I'm trying to achieve (Column B).

    Any help would be greatly appreciated
    Attached Files Attached Files

  2. #2
    please see this workbook and run the function.
    sorry my browser does not allow me to attached to this site.
    https://www.dropbox.com/scl/fi/y4sdc...mahdpz0vg1x7xu

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Try this

    Option Explicit
    
    
    Sub LinInterp()
        Dim rKnown As Range, rGap As Range
        Dim dLow As Double, dHigh As Double, dIncr As Double
        Dim cntGapCells As Long, iArea As Long, iGap As Long
        
        Set rKnown = ActiveSheet.Columns(1).SpecialCells(xlCellTypeConstants, xlNumbers)
    
    
        With rKnown
            For iArea = 1 To .Areas.Count
                .Areas(iArea).Cells(1, 2).Value = .Areas(iArea).Cells(1, 1).Value
            Next iArea
            
            
            For iArea = 1 To .Areas.Count - 1
                cntGapCells = .Areas(iArea + 1).Cells(1, 1).Row - .Areas(iArea).Cells(1, 1).Row - 1
                dLow = .Areas(iArea).Cells(1, 1).Value
                dHigh = .Areas(iArea + 1).Cells(1, 1).Value
                dIncr = (dHigh - dLow) / cntGapCells
            
                For iGap = .Areas(iArea).Cells(1, 1).Row + 1 To .Areas(iArea + 1).Cells(1, 1).Row - 1
                    ActiveSheet.Cells(iGap, 2).Value = ActiveSheet.Cells(iGap - 1, 2).Value + dIncr
                Next iGap
            Next iArea
            
        End With
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Try:
    Sub blah()
    For Each are In Columns("A:A").SpecialCells(xlCellTypeBlanks).Areas
      are.Resize(are.Rows.Count + 2).Offset(-1).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, Trend:=True
    Next are
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Regular
    Joined
    Dec 2016
    Posts
    22
    Location
    Thanks very much arnelgp, Paul_Hossler and p45scal for your replies

    arnelgp, that's brilliant, works exactly as intended! And same with your code also p45cal, just needed to add 'Dim are as Range' at the start to get it working - impressed by how you got it to work with just a couple of lines.

    Paul, that code works well, except it generates the same value as the known value for the previously-blank cell immediately above the known value.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Glad one of the suggestions works for you. That's the important thing

    I went back to learn where I went astray


    I was off by a "- 1" counting gapCells

                cntGapCells = .Areas(iArea + 1).Cells(1, 1).Row - .Areas(iArea).Cells(1, 1).Row (no "-1")
    Capture.JPG
    Last edited by Paul_Hossler; 09-09-2021 at 09:02 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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