首页 > 解决方案 > VBA如何显示两个带有符号的msgbox?

问题描述

Dim fg As Double
Dim msg1 As String
Dim msg2 As String
Dim result As Integer
Dim percent As Double


fg = Cells(8, 10).Value

percent = Application.WorksheetFunction.roundup(fg * 100, 2)


msg1 = "Fat as percentage of calories consumed:" & percent & "%"
msg2 = "Recommended total fat intake: less than 30% of total calories."
msg3 = "you are suggested to change your food choice to lower the fat"
If fg > 0.3 Then
    MsgBox msg1
    MsgBox msg2, vbExclamation
    result=MsgBox msg3, vbYesNo
    
    If result = vbYes Then
    ThisWorkbook.Sheets("Weekly Meal Planner").Activate
    end if
    If result = vbNo Then
    ThisWorkbook.Sheets("DAILY LOG").Activate
    End If
    
    
    
End If

If fg < 0.3 Then MsgBox (msg1)



End Sub

为什么当我单击“确定”时程序无法显示“每周膳食计划”表

标签: excelvba

解决方案


你不需要把它们放在同一条线上。

这是 if 语句的工作原理。

If fg > 0.3 Then 
  MsgBox msg2, vbExclamation
  MsgBox msg1
End If

MsgBox仅当您需要返回值时才使用括号:

Dim result as Variant
result = MsgBox(msg3, vbYesNo)

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-function

要将其用作Subroutine,请不要使用括号。

MsgBox "test"

要将其用作Function,请包含括号。

result = MsgBox("test")

您还可以使用图标和按钮选择:

result = MsgBox("Are you sure?", vbQuestion + vbYesNo)

推荐阅读