Reading Custom Formats

kkjensen

Active Member
Joined
Jun 22, 2004
Messages
354
Hi there,

Got some code where I'm trying to read the custom format of a cell. I've got a cell with units included as part of the custom format (either pounds or kg) and I need to create a statement in a VBA function that could read this added bit of text as a string so I can continue with the function according to what the formatting of that cell was.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Use the .NumberFormat property for range objects. eg:
Code:
    If InStr(1, UCase(rngCell.NumberFormat), "KG") Then
        MsgBox "It's kilos", vbInformation, "Nasty evil metric units"
    Else
        MsgBox "It's pounds", vbInformation, "Good old English measure"
    End If
 
Upvote 0
Looks good...thank goodness everyone in aerospace uses those nasty, evil units that make sense...the world would be such a better place if everyone just used fractions for all measurements and fancy sounding units like SLUGS! :)
 
Upvote 0
One last question. I'm using this bit of code in a defined function which seems to work fine. I'd like to add one last bit of code to custom format the cell where the function is being called from to display the same sort of custom formatting "kg" or "lbs". I've tried recording myself doing something but it doesn't do it:

Selection.NumberFormat = "0.00 "" lbs"""
Activecell.NumberFormat = "0.00 "" lbs""" doesn't work either.
 
Upvote 0
Hmm...If Peter Piper picked a decaliter of peppers where's the decaliter of peppers Peter Piper picked...28 grams of prevention is worth .45 kilos of cure...walk a 1.6 kilometers in another man's shoes...just ain't the same...
 
Upvote 0
Sorry, gotta scoot -- but that should work (what you posted). If you can't get 'er solved by tomorrow I'll stop by again.

Regards,
 
Upvote 0
Hilarious...I didn't know a PECK actually was something...

Metric bashing aside (I'll heal quickly) can a function format a cell?
 
Upvote 0
I'm trying to write a small function to convert things (I know there is a CONVERT function in the analysis toolpack but I don't want to have to tell clients how to install it and I can build more units into my formula (i.e. it might be pointed at lbs instead of inches) so I can just include the function with the spreadsheet to make sure that the equivalent value is always present.

Here's what I've got so far...the last line about formatting is the only one that doesn't work.

Code:
Function CUSTOMCONVERT(CELLREF As Range)

FormatCheck = CELLREF.NumberFormat

' detect units used
If InStr(1, FormatCheck, "in", 1) <> 0 Then
    LengthUnit = "in"
ElseIf InStr(1, FormatCheck, "m", 1) <> 0 Then
    LengthUnit = "m"
End If
  
' select output units
If LengthUnit = "m" Then
    outputLengthunit = "in"
    convfactor = 39.3701
End If

If LengthUnit = "in" Then
    outputLengthunit = "m"
    convfactor = 0.0254
End If

If convfactor = Empty Then
    CUSTOMCONVERT = "No Unit Detected"
Else
    CUSTOMCONVERT = CELLREF * convfactor
End If

ActiveCell.NumberFormat = "0.00 "outputLengthunit""

End Function

Activecell and Selection don't really work since this is just a function and not a macro.
 
Upvote 0
Nope, functions cannot alter cells. They can only return values. I can't recall if it was Damon, Richie, Ivan or Juan Pablo, but one of the boys that knows more about this stuff than I wrote up a nice explanation once. The gist of it is that altering cells in UDF's makes tracking precendence and dependence between cells all but impossible. So it is verboten.

(Four pecks to a bushel as I recall, but don't quote me on that.)
 
Upvote 0

Forum statistics

Threads
1,211,960
Messages
6,105,081
Members
447,947
Latest member
OX_2005

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