简体   繁体   English

基于另一个单元格值的自定义货币格式

[英]Custom currency format based on another cell value

In cell A1 i might have one of the following currencies "EUR", "USD" or "RON". 在单元格A1中,我可能具有以下货币之一“ EUR”,“ USD”或“ RON”。 In cell B1 i have the following custom cell format: "EUR" * 0.00"/mt" 在单元格B1中,我具有以下自定义单元格格式:“ EUR” * 0.00“ / mt”

Can anyone help in telling me how can i set the format in cell B1 to adapt taking into account the value from A1. 任何人都可以帮助我告诉我如何设置单元格B1中的格式以适应 A1的值。 The code must run only at workbook.open 该代码只能在workbook.open运行

Thank you in advance! 先感谢您!

Place the following Event Macro in the worksheet code area: 将以下事件宏放在工作表代码区域中:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A1 As Range, B1 As Range, BaseFormat As String
    Dim temp As String

    Set A1 = Range("A1")
    Set B1 = Range("B1")
    BaseFormat = """EUR"" * 0.00""/mt"""
    If Intersect(A1, Target) Is Nothing Then Exit Sub
    temp = A1.Value
    B1.NumberFormat = Replace(BaseFormat, "EUR", temp)
End Sub

Because it is worksheet code, it is very easy to install and automatic to use: 因为它是工作表代码,所以非常易于安装和自动使用:

  1. right-click the tab name near the bottom of the Excel window 右键单击Excel窗口底部附近的选项卡名称
  2. select View Code - this brings up a VBE window 选择查看代码-这将打开一个VBE窗口
  3. paste the stuff in and close the VBE window 将内容粘贴并关闭VBE窗口

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 如果您在2003年以后使用Excel版本,则必须将文件另存为.xlsm而不是.xlsx

To remove the macro: 删除宏:

  1. bring up the VBE windows as above 如上调出VBE窗口
  2. clear the code out 清除代码
  3. close the VBE window 关闭VBE窗口

To learn more about macros in general, see: 要总体上了解有关宏的更多信息,请参见:

http://www.mvps.org/dmcritchie/excel/getstarted.htm http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx http://msdn.microsoft.com/zh-CN/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see: 要了解有关事件宏(工作表代码)的更多信息,请参见:

http://www.mvps.org/dmcritchie/excel/event.htm http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work! 必须启用宏才能使其正常工作!

(to run this only at workbook open, have the Workbook Open macro re-assert the value in A1 and then disable events) (要仅在打开工作簿时运行此命令,请使“工作簿打开”宏重新声明A1中的值,然后禁用事件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM