+ Reply to Thread
Results 1 to 5 of 5

VBA code to convert absolute to semi-relative referenced range

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2011
    Location
    EU
    MS-Off Ver
    Excel for Mac 2011
    Posts
    2

    VBA code to convert absolute to semi-relative referenced range

    I want to create a function in VBA that has a cell and a range as input like this:
    MyFunction (cell1, range1)
    ...
    End function
    Calling MyFunction with input parameters cell1 = A1 and range1 = $A$1:$A$10 MyFunction should evaluate the following formula:
    RANK(A1,$A$1:$A$10,0)+COUNTIF(A$1:A1,A1)-1
    The difficulty with this is that the input range is specified as an absolute reference ($A$1:$A$10) but the formula should use a semi-relative range in the count if statement (A$1:A1).

    Can someone please show me the code to do this?

    Many Thanks !
    Last edited by gpk; 10-31-2011 at 04:24 AM.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA code to convert absolute to semi-relative referenced range

    If you need a formula as a string, then something like this:
    Function gpk$(r As Range, rng As Range) 'RANK(A1,$A$1:$A$10,0)+COUNTIF(A$1:A1,A1)-1
    Dim s1$, s2$
    s1 = r.Address(0, 0) 'relative, like A1
    s2 = r.Address(1, 0) 'semi-relative, like A$1
    gpk = "=RANK(" & s1 & "," & rng.Address(1, 1) & ",0)+COUNTIF(" & s2 & ":" & s1 & "," & s1 & ")-1"
    End Function

  3. #3
    Registered User
    Join Date
    10-29-2011
    Location
    EU
    MS-Off Ver
    Excel for Mac 2011
    Posts
    2

    Re: VBA code to convert absolute to semi-relative referenced range

    Thanks for your post. To clarify further: I do not want a string as output. Output should be the result of the formula, so in this case an integer.

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA code to convert absolute to semi-relative referenced range

    Let's try to use Evaluate. In any case, see attached file.
    Function gpk(rng As Range) 'RANK(A1,$A$1:$A$10,0)+COUNTIF(A$1:A1,A1)-1
    Dim s1$, s2$, i&, t()
    ReDim t(1 To rng.Cells.Count, 1 To 1)
    With rng
        s1 = .Cells(1).Address
        For i = 1 To rng.Cells.Count
            s2 = .Cells(i).Address
            t(i, 1) = Evaluate("RANK(" & s2 & "," & rng.Address & ",0)+COUNTIF(" & s1 & ":" & s2 & "," & s2 & ")-1")
        Next
    End With
    gpk = t()
    End Function
    Attached Files Attached Files
    Last edited by nilem; 10-29-2011 at 08:54 AM. Reason: use Evaluate

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA code to convert absolute to semi-relative referenced range

    function gpk(x,y,z)
      gpk=application.RANK(x,y,0)+COUNTIF(z,x)-1
    end funtion
    e.g. in cell B1
    PHP Code: 
    =gpk(A1,A1:A10,C1:C7
    PS. I have no idea what A$1:A1 is different from A1 or $A1 or $A$1 or A$1



+ 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