Referring to same column that contains UDF

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hey all,

I'm writing a UDF for a user on the boards here, and I have the need for the UDF to identify what column it is located in. Is there a way to do this without requiring an additional argument in the code?

Thanks in advance!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Code:
rng(rng.count)
or if your range might be discontiguous:
Code:
Function LastCellInRange(rngIn As Range) As Range
   With rngIn.Areas
      If .Count > 1 Then
         With .Item(.Count)
         Set LastCellInRange = .Cells(.Count)
         End With
      Else
         Set LastCellInRange = rngIn(rngIn.Count)
      End If
   End With
   
End Function
 
Last edited:
Upvote 0
Hmmm ...

Code:
Sub demo()
    MsgBox LastCellInRange(Columns("E")).Address
    MsgBox LastCell(Columns("E")).Address
End Sub
 
Function LastCellInRange(rngIn As Range) As Range
   With rngIn.Areas
      If .Count > 1 Then
         With .Item(.Count)
         Set LastCellInRange = .Cells(.Count)
         End With
      Else
         Set LastCellInRange = rngIn(rngIn.Count)
      End If
   End With  
End Function
 
Function LastCell(r As Range) As Range
    With r.Areas(r.Areas.Count)
        Set LastCell = .Cells(.Cells.Count)
    End With
End Function
 
Upvote 0
Should probably have tested. (or drunk less at lunch):
Code:
Function LastCellInRange(rngIn As Range) As Range
   With rngIn.Areas
      If .Count > 1 Then
         With .Item(.Count)
         Set LastCellInRange = .Cells(.Cells.Count)
         End With
      Else
         Set LastCellInRange = rngIn.Cells(rngIn.Cells.Count)
      End If
   End With
End Function
 
Upvote 0
Oh no. I try to sober up at least once a week. :)
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,930
Members
449,094
Latest member
teemeren

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