Here is a sample (for cell D1).
Say D1 has a formula that displays time:
and we only want to display the minutes. Put the following event macro in the worksheet code area:
Private Sub Worksheet_Calculate()
With Range("D1")
v = Minute(.Value)
f = .NumberFormat
If InStr(.NumberFormat, ";;;") > 0 Then
ActiveWorkbook.DeleteNumberFormat NumberFormat:=f
End If
.NumberFormat = Chr(34) & v & Chr(34) & ";;;"
End With
End Sub
Every time the worksheet is calculated, the macro adjusts the format of D1 to reflect only the minutes. (the code also removes previous custom number formats to prevent format everflow)
Because it is worksheet code, it is very easy to install and automatic to use:
- right-click the tab name near the bottom of the Excel window
- select View Code - this brings up a VBE window
- paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macro:
- bring up the VBE windows as above
- clear the code out
- close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
hh
part is needed (leading themm
) for Excel to display minutes instead of months, and thus, defeating the intended original purpose. However, the functionMINUTE
used on the time cell, may come in handy where you need the result. But that involves "extra transformation", I'm afraid.M
for month?12:30
) and you set formatting tomm
then Excel changes the cell value to a full date/time with a null date (month:1 day:0 year:1900), and results in displaying only01
as the month. In my opinion setting a format should NEVER change the cell value!