隐藏 DialogSheets [VBA]

Hiding DialogSheets [VBA]

我无法隐藏自定义对话框。按下按钮后(它们调用了其他宏),我的对话框仍然存在,如何隐藏它? 我在 "Dane_Makro" sheet 工作。 我以前的版本运行良好(我没有添加 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

结束子

答案已更新:

您需要向 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) 为什么你在代码中创建对话框而不是事先做它 B)为什么你使用 DialogSheet 而不是 UserForm)

旧答案:

因为一个DialogSheet是一个Sheet,你需要设置.Visible属性 到 xlSheetHiddenxlSheetVeryHidden

使用 .Hide 用于 UserForms,它取代了 DialogSheets 早在... 2000?