[英]Auto MsgBox when Excel file is opened and a condition is met
I suspect this is a pretty easy answer but I'm new to VBA and trying to generate a msgbox when I open my file and a certain condition is met. 我怀疑这是一个非常简单的答案,但是我是VBA的新手,并在我打开文件并满足特定条件时尝试生成msgbox。 My code, as it currently stands, is:
我目前的代码是:
Sub StopLossWarning()
If Range("C4").Value < 30 Then
MsgBox "Maximum allowable loss is at " & Range("C4").Value
End If
End Sub
The Msgbox appears fine when I run the macro. 当我运行宏时,Msgbox看起来很好。 I just need it to run once automatically when I open the file.
打开文件时,我只需要它自动运行一次即可。
Try this: 尝试这个:
Double click on ThisWorkbook from Project Explorer 在项目浏览器中双击ThisWorkbook
Copy the below code and paste there. 复制以下代码,然后粘贴到此处。
Private Sub Workbook_Open() Dim WK As Worksheet Set WK = Sheet1 'Change it to your sheet number. If WK.Range("C4").Value < 30 Then MsgBox "Maximum allowable loss is at " & WK.Range("C4").Value End If End Sub
Alternatively You can insert a new module and paste the code below there: 或者,您可以插入一个新模块,并将代码粘贴在此处:
Sub Auto_Open()
Dim WK As Worksheet
Set WK = Sheet1 'Change it to your sheet number.
If WK.Range("C4").Value < 30 Then
MsgBox "Maximum allowable loss is at " & WK.Range("C4").Value
End If
End Sub
Note: If you have multiple worksheet you may want to refer to ranges explicitly. 注意:如果您有多个工作表,则可能需要显式引用范围。 Otherwise you may get undesired result
否则您可能会得到不想要的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.