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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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