创build错误macros

我目前有一个macros,将提供一个字段留空的事件popup。 我所遇到的问题是,一旦你承认了错误,它就会消失,并让你在没有填补领域的情况下继续下去。 有没有人知道我在macros中缺less什么? 我不会发布整个脚本,因为它很长,但目前看起来像这样…

If Range("L58") = "Sigma Network" And Range("M58") = "" Then MsgBox "Please enter cable length " 

我可以创build一个适用于所有其他脚本的脚本吗?还是每个脚本都需要有自己的脚本?

很简单,就像:

 If Range("L58") = "Sigma Network" And Range("M58") = "" Then Range("M58").Value = InputBox("Please enter cable length ", "Input value!", 0) End If 

您当然需要额外的逻辑来防止用户input0值或空string等,

例如,如果你这样做,50个不同的单元对(例如L58到L107和M58到M107),这是一个基本的循环结构,你可以使用:

 Dim cl as Range For Each cl in Range("L58:L107") If cl.Value = "Sigma Network" and cl.Offset(0,1).Value = "" Then cl.Offset(0,1).Value = GetValue("Please enter cable length ", "Input value!", 0) End If Next 

循环可以进一步细化(例如,如果“西格玛networking”不是您正在检查的唯一东西,或者如果您需要基于其他某些条件为input框input不同的消息文本等)。

这将需要调用InputBox提示的自定义GetValue函数,并configuration为防止来自用户的0值input。 可能需要额外的逻辑来防止其他types的数据input。

 Function GetValue(msg as String, _ Optional title as String = "", _ Optional default as Double = 0) 'Function that calls the InputBox method Dim ret Do Until ret <> 0 ret = InputBox(msg, title, default) Loop GetValue = ret End Function