0

I created this function and it works to put the value into the cell but it doesnt work to set the .NumberFormat property.

Public Function NewYears(year As Integer)
    'Determine the cell that called the function
    Dim rng As range
    Set rng = ThisWorkbook.Sheets("How-To").range(Application.Caller.Address)
    MsgBox rng.Address
    fxFormat = "[$" & holidayName & "]"
    NewYears = DateSerial(year, 1, 1)
    rng.NumberFormat = fxFormat  
End Function

Update For more information:

I will be having functions like =NewYears() that returns a date.

I will do this for any number of holidays. I would like to format the field where it still stores the date but the .NumberFormat property has the name of the holiday

So =NewYears() would return "01/01/2014" but in the sheet it would appear as "New Years"

4

3 回答 3

0

Use the Worksheet_Change event:

Private Sub Worksheet_Change(ByVal Target As Range)
    Target.Interior.ColorIndex = 39
    Target.NumberFormat = "mm/dd/yyyy" 
End Sub

Put this code in the Sheets("How-To") code module. Modify to whatever color/etc that you want to format.

When you initially enter the function in the cell, it will trigger the change event and this subroutine will execute.

Per Gary's comments (below), recalculation of existing formula will not trigger this event.

于 2013-09-13T14:43:35.017 回答
0
  1. 您的工作表/书中有多少计算?

  2. 单元格是在特定排列的列中还是随处可见还是随机的?

    如果第一个问题的答案是肯定的,我不会建议使用 volatile 函数触发,那么没有人应该这样做。你接下来要做的事情取决于第二个问题的答案。

你为什么不尝试“条件格式”,虽然它可能有点贵。否则,如果“年份”单元格应该在有组织的列或单元格中,请确保其格式是迄今为止准备好的......如果这些都不适用,如果您的工作表的结构/设计,您可以给我们一个更好的图片......

于 2013-09-13T14:57:14.220 回答
0

函数只能返回值或操作注释,不能直接修改格式。

于 2013-09-13T14:09:29.110 回答