Loop problem through a range, UDF

hfler

Board Regular
Joined
Jun 10, 2008
Messages
95
Hi all,

I'm making a user defined function, where one of the inputs is a range. The range contains numbers that the UDF loops through and performs certain actions on. After a while, though, the range doesn't contain anymore numbers. For example,

<TABLE style="WIDTH: 48pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=64><COLGROUP><COL style="WIDTH: 48pt" width=64><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 48pt; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl63 height=20 width=64>432</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl63 height=20>4</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl63 height=20>3</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl63 height=20>23423</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl63 height=20>-</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl63 height=20>-</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl63 height=20>-</TD></TR></TBODY></TABLE>


I'm trying to figure out how to select the last cell in the range that contains a number once the loop starts going into ranges that don't contain numbers. In a sub, I think I'd using something along the lines of,

Code:
Range(range_no_1).xlDown.Select

But I don't know how to do that in a UDF. Any help is much appreciated. Thanks!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello,

Quote:
Code:
Range(range_no_1).xlDown.Select

You might want to try this:

Code:
Sub LastCellInColumn()
Dim LastCell As Long
Dim ColNum As Long
 
ColNum = InputBox("Which Column?", "Last Cell In Column", Default)
LastCell = Cells(Rows.Count, ColNum).End(xlUp).Row
 
Cells(LastCell, ColNum).Select
 
End Sub

You can also put this part of the code within your own code:

Code:
Dim LastCell As Long
Dim ColNum As Long
 
ColNum = InputBox("Which Column?", "Last Cell In Column", Default)
LastCell = Cells(Rows.Count, ColNum).End(xlUp).Row
 
Upvote 0
Thanks bubbapost,

That seems to work for a sub. For a UDF, though, how could you select the last cell with a number in it for a highlighted range? So for instance, I would have a formula structured like,

Code:
Function test(date as double, testvalue as double, variation as range)as variant

How could I loop through the variation range, and select the last cell in that range that contained a number?

Thanks again!
 
Upvote 0
You can't select a cell in a UDF. With some odd exceptions, all youu can do is return a value to the cell in which it appears.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

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