Word:创建公式以减去Word中的日期 [英] Word: Create formula to subtract dates in Word

查看:767
本文介绍了Word:创建公式以减去Word中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Word 2010中有一个表单,正在尝试输入两个日期并获得以天为单位的差额.我尝试创建一个具有一行三列的表.在第一列和第二列中输入数字,而不是日期,在第三列中输入公式=B1 - A1.使用数字可以使用,但是不能使用日期.

I have a form in Word 2010 and am trying to enter two dates and get the difference in days. I tried creating a table with one row and three columns. Instead of dates I entered numbers in the first and second column and in the third I entered the formula =B1 - A1. With numbers it works, but not with dates.

是否可以输入两个日期并获得Wo​​rd 2010中的天数差异?我知道我可以轻松地在Excel中执行此操作,但是由于该表单的范围很广,所以我想尝试在Word中执行此操作.

Is it possible to enter two dates and get the difference in days in Word 2010? I know I can easily do this in Excel, but since the form is extensive I would like to try to do it in Word.

推荐答案

在Word 2010中,至少有3种类型的内联表单".它们全部都可以用来检测VBA中字段的更改.

There are at least 3 types of "inline forms" in Word 2010. All of them let you detect changes to fields in VBA.

  • 如果您使用的是旧版ActiveX表单(通常不建议使用这些 天),您可以使用与每个控件相关的事件
  • 如果您使用的是所谓的旧版表格",则可以使用 为每个字段输入Enter和On Exit宏以调用VBA
  • 如果您使用内容控件,则可以使用内容控件 事件.
  • If you are using the old ActiveX forms (not usually recommended these days) you can use the events associated with each control
  • If you are using the so-called "legacy forms" you can use the On Enter and On Exit macros for each field to invoke VBA
  • If you are using content controls, you can use the content control events.

在这三个选项中,任何版本的Mac Word都只能使用旧版表单,而Mac Word 2008上不可以使用VBA.

Of those 3 options, only legacy forms are available on any version of Mac Word, and VBA is not avaiable on Mac Word 2008.

如果要避免使用VBA并使用字段,则有两个主要注意事项:

If you want to avoid VBA and use fields, there are two main considerations:

  1. 公式字段{=}中没有日期函数或运算符.
  2. 表格单元格引用(参见A1,B1等)也仅适用于 数字,而不是日期.
  1. There are no date functions or operators in the formula field { = }.
  2. The table cell references (cf. A1, B1 etc.) also only work with numbers, not dates.

您可以通过将日期转换为儒略日数字,然后使用{=}减去结果数字,来解决第(1)点的问题.有关建议的代码,请参见下文.

You can work around point (1) by converting the dates to Julian Day Numbers, and using { = } to subtract the resulting numbers. See below for some suggested code.

只要使用表单字段输入日期,就可以避免第(2)点中的问题.然后,您可以使用表单字段的书签名称获取文本,并使用日期字段开关"\ @"获取日期.月和年的数字.

You can avoid the problem in point (2) as long as you use form fields to enter the dates. Then you can use the form field's bookmark name to get the text and the date field switch "\@" to get the day. month and year numbers.

因此,假设您的两个日期表单字段分别称为DA和DB.然后,您可以在文档中想要结果的位置插入以下域代码-请注意,所有{}对都必须是特殊的域代码花括号,您可以使用ctrl-F9输入.

So let's suppose your two date form fields are called DA and DB. Then you can insert the following field codes at the point in the document where you want the result - notice that all the { } pairs have to be the special field code braces that you can enter using ctrl-F9.

{ SET MA { DA \@M } 
}{ SET CA { =INT((14-MA)/12) } 
}{ SET XD { ={ DA \@YYYY }-CA } 
}{ SET XC { =INT(XDA/100) } 
}{ SET XB { =XD-XC*100} 
}{ SET XA { =MA+12*CA-3 } 
}{ SET JA { =INT(146097*XC/4) + INT(36525*XB/100) + INT((153*XA+2)/5) + { DA \@D } + 1721119 } 
}{ SET MB { DB \@M } 
}{ SET CB { =INT((14-MB)/12) } 
}{ SET YD { ={ DB \@YYYY }-CB } 
}{ SET YC { =INT(YD/100) } 
}{ SET YB { =YD-YC*100 }
}{ SET YA { =MB+12*CB-3 } 
}{ SET JB { =INT(146097*YC/4) + INT(36525*YB/100) + INT((153*YA+2)/5) + { DB \@D } + 1721119 } 
}{ =JB-JA }

一些注意事项:

  • 这些域代码基于此处
  • 变量名称已稍作更改(X1-> XA等),并且 因为没有楼层,所以计算已稍作更改 功能,虽然有MOD功能 如果您想让现场编码正常工作,最好避免 国际上.
  • 在这种情况下,您可以省略两个"-1721119"表达式.
  • 如果您删除了上面表达式中的大多数空格, 更喜欢 我在各行的开头用} {"布置了这些字段,因为这种方法在显示字段结果时会隐藏段落标记.
  • 请注意,预览/打印时可能会出现问题,具体取决于 有关Word的字段更新设置.
  • These field codes are based on the calculations provided here
  • The variable names have been changed a little (X1->XA etc.), and the calculations have been changed a little because there is no FLOOR function in the field language, and although there is a MOD function it is better avoided if you want your field coding to work internationally.
  • In this case, you could omit the two "-1721119" expressions.
  • You can remove most of the spaces in the above expressions if you prefer I have laid out the fields with "}{" at the beginnings of lines, because that approach hides the paragraph marks when the field results are displayed.
  • be aware that there can be a problem when you preview/print depending on Word's field updating settings.

"macropod"撰写的关于使用字段进行日期计算的文章更加完整.它使用的算法略有不同(并且可能更短),但是本文还提供了朝另一个方向发展的算法,依此类推.我认为您必须登录从此处获取副本,但您也许可以在其他地方找到它.

There is a much more complete paper by "macropod" on using fields for date calculations. It uses a slightly different (and probably shorter) version of the algorithm, but the paper also provides algorithms for going in the other direction, and so on. I think you have to log in to get a copy from here , but you may be able to find it elsewhere.

这篇关于Word:创建公式以减去Word中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆