+ Reply to Thread
Results 1 to 5 of 5

Matching a value to a column, (rounding) and returning offset column value

Hybrid View

jayinthe813 Matching a value to a column,... 03-19-2013, 11:50 AM
Richard Buttrey Re: Matching a value to a... 03-19-2013, 12:28 PM
hafizimran Re: Matching a value to a... 03-19-2013, 12:37 PM
jayinthe813 Re: Matching a value to a... 03-19-2013, 01:50 PM
Richard Buttrey Re: Matching a value to a... 03-19-2013, 02:42 PM
  1. #1
    Forum Contributor
    Join Date
    08-01-2012
    Location
    Tampa
    MS-Off Ver
    Excel 2010
    Posts
    121

    Matching a value to a column, (rounding) and returning offset column value

    I am dealing with 2 columns like below. What I want to do, via VBA, is to provide a number, say $3050, match it against column A (rounding up if necessary) and returning the item from column B.


    For instance

    mystring = "3150"
    Value (COL A) Perecentage (COL B)
    $3,000 95%
    $3,100 94.8%
    $3,200 94.6%
    $4000 94.4%

    3150, should return 94.6%. It should always round up and numbers should always be between 3000-15000, I know my qualifier is:
    If mystring > 3000 and mystring < 15000 Then
    
    'do stuff here
    
    end if
    just not sure about how I should try to match the values up, thanks!

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Matching a value to a column, (rounding) and returning offset column value

    Hi,

    Do you really need a macro. With your chosen number in say D1 then
    Formula: copy to clipboard

    =INDEX(B:B,MATCH(D1,A:A,1)+1,1)

    will give you your answer. If you really do need a macro then just tuck this formula away somewhere and read the cell value directly into your VBA variable.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    12-31-2012
    Location
    Jhang, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Matching a value to a column, (rounding) and returning offset column value

    Function MatchValues(myString As String)
        Dim i As Long
        'Supposing that your data is in A and B columns starting from 2nd row
        For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
            If Range("A" & i).Value >= Val(myString) Then
                MatchValues = Format(Range("B" & i).Value, "Percent")
                Exit Function
            End If
        Next
    End Function

  4. #4
    Forum Contributor
    Join Date
    08-01-2012
    Location
    Tampa
    MS-Off Ver
    Excel 2010
    Posts
    121

    Re: Matching a value to a column, (rounding) and returning offset column value

    Thanks to both of you! I could do either one but im automating with access and vba so the vba alternative makes more sense I think

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Matching a value to a column, (rounding) and returning offset column value

    Hi,

    Do either of these help. One is a function, so enter say
    =Percentage(3150)
    in a cell.

    The other is an equivalent procedure which will result in the dResult value showing the percentage.

    Function Percentage(lmyvalue As Long) As Variant
        Dim lRow As Long
        If lmyvalue > 3500 And lmyvalue < 15000 Then
            lRow = WorksheetFunction.Match(lmyvalue, Range("A:A"), 1)
            Percentage = Application.WorksheetFunction.Index(Range("B:B"), lRow, 1)
            Else
            Percentage = "Outside limits"
            
        End If
    End Function
    Sub GetPercentage()
        Dim lRow As Long, lmyvalue As Long, dResult As Variant
    
        lmyvalue = 3150    ' or use an input box to get a value
        If lmyvalue > 3500 And lmyvalue < 15000 Then
    
            lRow = WorksheetFunction.Match(lmyvalue, Range("A:A"), 1) + 1
            dResult = Format(Application.WorksheetFunction.Index(Range("B:B"), lRow, 1), "0.000")
        Else
            dResult = "Outside limits"
        End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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