Is Application.Volatile the correct solution?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
With help here from Rick Rothstein and Jerry Sullivan, I wrote a UDF (col()) to provide a column function similar to the built-in row() function. I later discovered that its results were not getting updated when a column was inserted before where it was called, causing the column letter to be off.

After little research, I added an Application.Volatile statement to the UDF and now it always gets updated. Is that the correct solution?

Here's the UDF:
Code:
Function col(Optional ByVal BaseCell As Range) As String
Application.Volatile    'Force recalculation on any update
 
Dim NumCols As Long
Dim CallerCell As Range
Set CallerCell = Application.Caller
 
With Application.Caller
  If BaseCell Is Nothing Then 'If no basecell, return the actual column letter
    col = Split(CallerCell.Address, "$")(1)
  Else                        'Else, return the column letter relative to the basecell
    NumCols = .Column - BaseCell.Column 'Get the displacement from the basecell
    If NumCols > 0 Then                 'If away from the basecell, return the displacement
      col = Split(.Parent.Cells(1, NumCols).Address, "$")(1)
    Else                                'Else, return "**"
      col = "**"
    End If
  End If
End With

End Function
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Application.Volatile does the job well :)

How about this variant ...

Code:
Function Col(Optional ByVal BaseCell As Range) As String
    Application.Volatile  
    If BaseCell Is Nothing Then Set BaseCell = Application.Caller
    Col = Split(BaseCell.Address, "$")(1)
End Function
 
Upvote 0
Application.Volatile does the job well :)
Thanks

How about this variant ...
Code:
Function Col(Optional ByVal BaseCell As Range) As String
    Application.Volatile  
    If BaseCell Is Nothing Then Set BaseCell = Application.Caller
    Col = Split(BaseCell.Address, "$")(1)
End Function
Wow!!! I love simple, tight code. I'll do some testing to ensure I understand it.

Thank you! (y)
 
Upvote 0
in my version of EXCEL (2007) there is a perfectly good inbuilt function which does the same : Column()
Has this been dropped in more recent versions??
(I have just spotted the different you want the letter!!)
 
Last edited:
Upvote 0
@offthelip
=IF("You try the function", "You may learn something", "You will miss out") :biggrin:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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