【问题标题】:Hiding DialogSheets [VBA]隐藏 DialogSheets [VBA]
【发布时间】:2018-04-25 00:03:28
【问题描述】:

我在隐藏我的自定义对话框框架时遇到问题。按下按钮后(他们调用了其他宏),我仍然有我的对话框框,如何隐藏它? 我正在使用“Dane_Makro”表。 我以前的版本运行良好(我没有添加 2 个额外的按钮,但我编辑了 vbYesNo 按钮 - 方案非常相似) 问候

Source of dialogsheets

'Public btnDlg As DialogSheet

Sub CallBots()

Dim btnDlg As DialogSheet
Dim ButtonDialog As String
ButtonDialog = "CustomButtons"
Dim klik As Boolean
klik = True


Dim oSHL As Object: Set oSHL = CreateObject("WScript.Shell")

Application.ScreenUpdating = False
Application.EnableEvents = False


On Error Resume Next
Application.DisplayAlerts = False
ActiveWorkbook.DialogSheets(ButtonDialog).Delete
Err.Clear
Application.DisplayAlerts = True

Set btnDlg = ActiveWorkbook.DialogSheets.Add


With btnDlg
    .Name = ButtonDialog
    .Visible = xlSheetHidden

    With .DialogFrame
        .Height = 70
        .Width = 300
        .Caption = "Generowanie plików do BOTÓW"
    End With

    .Buttons("Button 2").Visible = False
    .Buttons("Button 3").Visible = False
    .Labels.Add 100, 50, 100, 100
    .Labels(1).Caption = "Jak utowrzyć pliki wsadowe do botów?"

    .Buttons.Add 220, 44, 130, 18 'Custom Button #1,index #3
    With .Buttons(3)
        .Caption = "Nowe pliki wsadowe"
        .OnAction = "MakeBotsNew"
    End With


    .Buttons.Add 220, 64, 130, 18 'Custom Button #2,index #4
    With .Buttons(4)
        .Caption = "Konsolidacja plików wsadowych"
        .OnAction = "MakeBotsConso"
   End With


    If .Show = False Then
        oSHL.PopUP "Anulowanie procesu", 1, "Tworzenie plików", vbInformation
        klik = False
    End If

End With

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    'On Error Resume Next
    DialogSheets("CustomButtons").Delete
    Err.Clear
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    btnDlg.Visible = xlSheetVeryHidden

    'Application.ScreenUpdating = True
    Application.EnableEvents = True

结束子

【问题讨论】:

    标签: vba excel userform


    【解决方案1】:

    答案更新:

    您将需要向您的 MakeBotsNewMakeBotsConso 宏添加一个(可选?)参数,如下所示:

    Sub MakeBotsNew(Optional Name As String = "")
        'Existing Code here
    
        'After existing code:
        If Len(Name) > 0 Then ThisWorkbook.DialogSheets(Name).Hide 'Hide dialog box
    End Sub
    

    然后,您需要将 ButtonDialog 名称作为参数添加到 .OnAction,这也意味着将其用单引号括起来:

    .OnAction = "'MakeBotsNew """ & ButtonDialog & """'"
    

    (我仍然不明白 A) 为什么您在代码中创建 Dialog 而不是预先创建,以及 B) 为什么您使用 DialogSheet 而不是用户窗体)

    旧答案:

    由于 DialogSheet 是 Sheet,您需要将 .Visible 属性设置为 xlSheetHiddenxlSheetVeryHidden

    使用 .Hide 是为了 UserForms,它在 2000 年取代了 DialogSheets?

    【讨论】:

    • 我已将.Hide 替换为.Visible = xlSheetHidden 仍然无法正常工作。找错地方了?
    • 注释掉你的On Error Resume Next 行,或者添加一个On Error GoTo 0 行,看看它是否给你一个错误信息,告诉我们哪里出了问题
    • 我在代码中做了一些更正。总而言之,我的On Error Resume Next 只是保护(检查 dialogsheets("custombuttons") 是否存在。所以,代码有效。按 X 使其不显示(完美)。但按按钮 #3 或按钮 #4 不能使我的对话框框架不可见,已删除等。
    • 添加了有关如何执行此操作的说明,方法是将表单名称作为要在对象集合中使用的参数传递给 .Hide it
    • 你是我亲爱的上帝。 PS。我为什么要那样做?答案很简单——不同的人(:))会使用这个工具,我关心我的存储库中的动态更新宏。学习新事物总是好的,因此感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多