Setting NumberFormat for a cell via a VBA function

elisag

New Member
Joined
Jun 17, 2024
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I'd like to be able to set the display format of data for a cell (for example the caller cell) via a VBA function and I've found posts around the internet that seem to suggest it to be feasible and indeed it is feasible via a macro on a hardcoded cell (for example cell A1) but all my attempts to do it via a function (or a sub that the function calls) have failed. In my code I'm not getting any error, but apparently my setting of NumberFormat seems to be ignored. The fact that setting NumberFormat is ignored is not completely true because when in the string format I provide is present the percent character such as in "0.00%" the new NumberFormat is indeed applied but this is the only case where the thing works. I have also tried using the trick/workaround of invoking the evaluate function to call a sub that sets the NumberFormat for the desidered cell but it does not allow achieving the intended result.

Below is an example code I used in one of the many attempts I made.

VBA Code:
Function SetFormatFunction()

    SetFormatSub Application.Caller, 1.1111
    
End Function

Private Sub SetFormatSub(cell As Range, val As Double)
    
    cell.NumberFormat = "0.0;[Red]-0.0;0;na;"
    cell.Style.NumberFormat = "0.0;[Red]-0.0;0;na;"
    
End Sub
 
At this point what I do not understand then is in what context have the code samples I see around proposed for similar needs been used considering that nothing that runs in VBA code can achieve this result (apart macros but only executed directly from the user, not any executed programmatically)? The NumberFormat function used in a macro seems to me pretty useless, why even provide it at this point?
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
It's only UDFs called from a cell that have that restriction. If you use NumberFormat in other code, such as macros or event routines, it will work just fine.
 
Upvote 0
It is all about using the correct tools with the correct options. Functions and Sub Procedures have different purposes and different rules.
Just like you can use cars and boats as means of transportation, but wouldn't use a car to cross a lake.
 
Upvote 0
@Joe4 I'm not aware of many other options other than ribbons (vsto and vba) or addins (xll). @RoryA when you say routines what do you mean?
 
Upvote 0
See here for an explanation on the differences between Sub Procedures and User Defined Functions.
They are both VBA, but have different properties, rules, and uses.
 
Upvote 0
@Joe4 I believe I know the difference between a function and a sub and when in the context of excel someone says routine I think of subs. Macros are parameter-less subs but when calling subs (with or without parameters) from a function they inherit the context of the calling function. I tried .NET UDFs and calling VBA subs from such a UDF and the behavior is the same of calling them from a VBA UDFs. I remember that VSTO ribbons could write anywhere (any cell, any sheet) and set backgrounds, formatting, conditional formatting, ecc.....
 
Upvote 0
It's only code called from a cell that has these restrictions. If you use a macro or something like a worksheet_change or worksheet_calculate event, there are no such restrictions.
 
Upvote 0
I understand, but code that populates a cell is, I believe, the ideal place to also format data in that same cell, isn't it? 😂
 
Upvote 0
It may be but, as I said, a UDF doesn’t actually populate a cell. It just returns a value
 
Upvote 0
I understand, but code that populates a cell is, I believe, the ideal place to also format data in that same cell, isn't it? 😂
Can you name any native Excel function that does that?
That formats the cell that you put it in?

I think we both know that the answer is "No".
So why would you expect your own custom Function to do that?
To have some property that the native Excel functions do not?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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