Search data for closest value, Offset 1 column same row as value, calculate weighted average of x number of values above and below found value

jzannoni

New Member
Joined
Nov 4, 2016
Messages
7
In an excel sheet I have two rows of data. I want to search the first row for the closest value of what the user inputs into the box and then do a weighted average of five values in the column next to the column I searched. The values are all in the column to the right of the searched column and are the two rows above, the same, row and the two rows below the row of the value found in the search column. The length of the data will vary from data set to data set but the two columns within a given set will be the same length. Right now the code searches and finds the correct row and column for the closest value and outputs it as a string with the line oAd=Dn.address. I cannot get the string cell reference to change back into a range reference so that I can offset and assign values to variables to calculate the weighted average. It would be easier to have oAd=Dn.address not to be a string to begin with but I cannot figure out how to do that either. I pieced what I have together from other questions and excel help files. I know there is a better way to do this or even a simple way to fix the convoluted code I have written. I appreciate any help

Code:
Sub MG10Jun26()
Dim num As Single, Rng As Range, Dn As Range, Mx As Single, oAd As Range
Dim Nd As Range


On Error Resume Next
 num = Application.InputBox(prompt:="Insert Number ", Title:="Find closest Number", Type:=1)


Set Rng = Range(Range(1, 0), Range("A" & Rows.Count).End(xlUp))
    Mx = Application.Max(Rng)


    For Each Dn In Rng
        If Abs(num - Dn) < Mx Then
            Mx = Abs(num - Dn)
            oAd = Dn.Address
        End If
    Next Dn
Nd = Range(oAd)
Par = Nd.Offset(0, 1).Value
Paru1 = Par.Offset(-1, 0).Value
Paru2 = Par.Offset(-2, 0).Value
Pard1 = Par.Offset(1, 0).Value
Pard2 = Par.Offset(2, 0).Value
weightavg = (0.25 * Paru2 + 0.5 * Paru1 + 1 * Par + 0.5 * Pard1 + 0.25 * Pard2) / 2.5
MsgBox weightavg
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top