消息框答案被忽略了?

时间:2018-01-10 22:32:11

标签: excel vba excel-vba messagebox

我有一个用于打印报告的ActiveX命令框。用户可以选择打印紧凑版本。如果是,则代码应隐藏一些行并将工作表设置为纵向方向。如果不是,则使用工作表的默认值打印报告。

截至目前,无论您点击什么,都会使用工作表的默认值而不是编码参数打印完整报告。我该如何解决这个问题?

Private Sub SummarizedReport_Click()

Application.ScreenUpdating = False

Dim Answer As Integer

ActiveWorkbook.Unprotect


MsgBox "Would you like to Print a Compact Version of this Estimate?", vbYesNo + vbQuestion, "Print Dialogue"

    If Answer = vbYes Then

        Worksheets("SUMMARY").Unprotect
        Worksheets("SUMMARY").Rows("F:I").Hidden = True
        Worksheets("SUMMARY").PageSetup.Orientation = xlPortrait
        Worksheets("SUMMARY").PageSetup.PaperSize = xlPaperLetter

Sheets("Cover Page").Visible = -1

ActiveWorkbook.Sheets(Array("Cover Page", "SUMMARY")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, IgnorePrintAreas:=False, OpenAfterPublish:=True

        Sheets("Cover Page").Visible = 2

    Sheets("SUMMARY").Rows("F:I").Hidden = False
    Sheets("SUMMARY").Protect


    Else

        Sheets("Cover Page").Visible = -1

ActiveWorkbook.Sheets(Array("Cover Page", "SUMMARY")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

        Sheets("Cover Page").Visible = 2

    End If

ActiveWorkbook.Protect

Worksheets("Control").Activate

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:1)

以下是绊倒你的两件事。 在声明中的顶部。

Dim answer As VbMsgBoxResult

当您去查询用户的答案时:

answer = MsgBox ("Would you like to Print a Compact Version of this Estimate?", vbYesNo, "Print Dialogue")

然后根据

运行您的代码
If answer = vbYes Then
'your code for yes
Else 'assumes it was no.
'your code for no
End if