Mimicking ROW() function using VBA

petereddy

New Member
Joined
Feb 6, 2014
Messages
43
Hi,

I'm trying to broaden my horizons. Is there a way to mimic Excel's ROW() function in VBA? The following code is close:
Code:
Function MYROW()
    MYROW = Selection.Row
End Function
But if you have a range that spans more than two rows and hit Ctrl + Enter, it will only output the top row of the range.

That's not its only bug. For example, if you use it on a single cell (say A10, in which case the result is correctly 10), then select a row and delete it (say 5:5), MYROW() will take on the row number of the deleted row (5, in this example).

If you're up for it, is there a way to design a formula that outputs the formula of a targeted cell? Like my ROW()-mimicking function above, the following code is a start, but it has similar drawbacks:
Code:
Function GETFORMULA(ByVal Address)
    Dim myformula, leftmostbracket, rightmostbracket, mytarget
    myformula = Selection.Formula
    leftmostbracket = Application.Find("(", myformula)
    rightmostbracket = Application.Find(")", myformula)
    mytarget = Mid(myformula, leftmostbracket + 1, _
        rightmostbracket - leftmostbracket - 1)
    GETFORMULA = Range(mytarget).Formula
End Function
Is there a way to do this?

Thanks for your help.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I don't understand what you are trying to do with the ROW() function, but you can re-write your GetFormula UDF like
Code:
Function GETFORMULA(Cl As Range)
    GETFORMULA = Cl.Formula
End Function
 
Upvote 0
You're welcome & thanks for the feedback.

If you are suing Xl 2013 or newer you can just use the FORMULATEXT function, which will do the same thing.
 
Upvote 0
Good to know. I'm currently on Office 2010. We're moving to 2016 soon, so I'll keep that in mind when the upgrade takes place.
 
Upvote 0
Regarding MyRow. It is keyed with the Selection, which is odd. If you want the UDF to refer to the cell holding the formula, you could use Application.Caller

Code:
Function MyRow2(optional aRange as Range) As Long

    If aRange is Nothing Then
        Set aRange = Application.Caller
    End Ifr

    MyRow2 = aRange.Row
End Function

When passed a multi-row range, Excel''s ROW returns the row number of the top-left cell. It sounds like you want something different than that.
 
Upvote 0
Thanks for pointing out the Caller property. I was wondering if there was something like that. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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