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.
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