简体   繁体   English

Excel 公式,如果 B1 有文本,则在 A1 中输入今天的日期

[英]Excel formula for enter today date in A1 if B1 has a text

Could you help me please to find out an excel formula to enter today date in A1 if B1 has a specific word?如果 B1 有一个特定的单词,你能帮我找出一个 excel 公式在 A1 中输入今天的日期吗?

♣Solved!♣ ♣解决了!♣

2 proposed solns/alternatives: 2 建议的解决方案/替代方案:


(1) Excel function (1) Excel function

Enter in cell A1:在单元格 A1 中输入:

=if("B1" = "whatever", today(),"") =if("B1" = "随便", 今天(),"")

To ignore updating, will need VB code most likely, or simply put Automatic Calc.要忽略更新,很可能需要 VB 代码,或者简单地放入 Automatic Calc。 off (tools, options, forumulas -> manual + untick 'recalc before saving') ie:关闭(工具、选项、论坛 -> 手动 + 取消勾选“保存前重新计算”)即:

关闭自动计算。避免自动更新


(2) VB (2) VB

VB code (open excel, press alt + f11 to open VB, select relevant sheet from within the 'Microsoft Excel Objects' dropdown list for workbook in question - you'll find this in the 'Project Explorer' window pane of VB, eg in my case, it's 'Sheet14 (Sheet1)" per screenshot below..): VB code (open excel, press alt + f11 to open VB, select relevant sheet from within the 'Microsoft Excel Objects' dropdown list for workbook in question - you'll find this in the 'Project Explorer' window pane of VB, eg in我的情况,它是“Sheet14(Sheet1)”下面的截图..):

VB代码

Enter following code after selecting "Worksheet" and "Change" from drop-down lists in the main code section that opens up after you double click (in my case): "Sheet14 (Sheet1)", per above:从双击后打开的主代码部分的下拉列表中选择“工作表”和“更改”后输入以下代码(在我的情况下):“Sheet14(Sheet1)”,如上:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("b1")) Is Nothing Then
    If Range("b1").Value = "whatever" Then
        Range("a1").Value = Date
    End If
Else
    'do Nothing
End If

End Sub

(note: some trigger is required - above code assumes this will be cell B1 changing to "whatever" specific text). (注意:需要一些触发器 - 上面的代码假定这将单元格 B1 更改为“任何”特定文本)。


References: (adapted this for 'change' as opposed to 'selectionchange':) using intersect with 'selectionchange' or equivalent/similar date function: Date function: VB参考文献:(将其改编为“更改”而不是“选择更改”:) 使用与“选择更改”或等效/类似日期相交 function:日期 function:VB


Pros/Cons (VB soln):优点/缺点(VB 解决方案):

Pro: Not effected by calculation updates or change in date clock events Pro:不受计算更新或日期时钟事件更改的影响

Con: Requires VB缺点:需要 VB

Con: More code required to remove content of cell A1 if B1 updated...缺点:如果 B1 更新,则需要更多代码来删除单元格 A1 的内容...

Pro: Auto-updates whenever cell changes Pro:每当单元格更改时自动更新


VB test results VB测试结果

测试结果 - VB

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

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