为什么我不能在进度栏上隐藏标题栏?

时间:2019-10-02 04:01:27

标签: excel vba progress-bar userform

我正在尝试使用here中的一些代码。 我已经在一些编程论坛上签到了,看来还可以。但是我在显示用作进度条的用户窗体时遇到438错误。我对如何使用库一无所知(这是我第一次)。

标题代码:

Option Explicit
Option Private Module

' FROM https://wellsr.com/vba/2017/excel/beautiful-vba-progress-bar-with-step-by-step-instructions/
Public Const GWL_STYLE = -16
Public Const WS_CAPTION = &HC00000
#If VBA7 Then
    Public Declare PtrSafe Function GetWindowLong _
                           Lib "user32" Alias "GetWindowLongA" ( _
                           ByVal hWnd As Long, _
                           ByVal nIndex As Long) As Long
    Public Declare PtrSafe Function SetWindowLong _
                           Lib "user32" Alias "SetWindowLongA" ( _
                           ByVal hWnd As Long, _
                           ByVal nIndex As Long, _
                           ByVal dwNewLong As Long) As Long
    Public Declare PtrSafe Function DrawMenuBar _
                           Lib "user32" ( _
                           ByVal hWnd As Long) As Long
    Public Declare PtrSafe Function FindWindowA _
                           Lib "user32" (ByVal lpClassName As String, _
                           ByVal lpWindowName As String) As Long
#Else
    Public Declare Function GetWindowLong _
                           Lib "user32" Alias "GetWindowLongA" ( _
                           ByVal hWnd As Long, _
                           ByVal nIndex As Long) As Long
    Public Declare Function SetWindowLong _
                           Lib "user32" Alias "SetWindowLongA" ( _
                           ByVal hWnd As Long, _
                           ByVal nIndex As Long, _
                           ByVal dwNewLong As Long) As Long
    Public Declare Function DrawMenuBar _
                           Lib "user32" ( _
                           ByVal hWnd As Long) As Long
    Public Declare Function FindWindowA _
                           Lib "user32" (ByVal lpClassName As String, _
                           ByVal lpWindowName As String) As Long
#End If

隐藏标题栏的程序

Sub HideTitleBar(frm As Object)
    Dim lngWindow As Long
    Dim lFrmHdl As Long

    lFrmHdl = FindWindowA(vbNullString, frm.Caption)
    lngWindow = GetWindowLong(lFrmHdl, GWL_STYLE)
    lngWindow = lngWindow And (Not WS_CAPTION)
    Call SetWindowLong(lFrmHdl, GWL_STYLE, lngWindow)
    Call DrawMenuBar(lFrmHdl)
End Sub

当我尝试显示Userform时,在行lFrmHdl = FindWindowA(vbNullString, frm.Caption)上遇到了一个恒定的438错误。我没找到原因。

我创建的用于调用此用户窗体的宏:

Sub Delete_Zero()
Dim LastRow As Long
Dim LastCol As Long
Dim i As Long
Dim j As Long
Dim pctdone As Single


'(Step 1) Display your Progress Bar
ufProgress.LabelProgress.Width = 0
ufProgress.Show vbModeless

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
For i = 1 To LastRow
    For j = 1 To LastCol
        If Cells(i, j).Value2 = 0 Or Cells(i, j).Value2 = "" Then
            Cells(i, j).ClearContents
        End If
    Next j
    '(Step 2) Update Progress Bar
    pctdone = i / LastRow
    With ufProgress
        .LabelCaption.Caption = "Processing Row " & i & " of " & LastRow
        .LabelProgress.Width = pctdone * (.FrameProgress.Width)
    End With
    ufProgress.Repaint
    '(Step 3) Unload Progress Bar
    If i = LastRow Then Unload ufProgress
Next i

End Sub

任何帮助将不胜感激。

更新1: 我忘了在用户表单中附加代码:

Private Sub UserForm_Initialize()
    Me.Height = Me.Height - 10
    HideTitleBar.HideTitleBar Me
End Sub

1 个答案:

答案 0 :(得分:1)

如果您只是想阻止用户单击X关闭按钮,则无需跳过一大堆来隐藏标题栏。只需捕获事件并取消请求即可。

在您的用户表单代码模块中,粘贴以下代码:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    Cancel = True

End Sub

这就是全部。


是否想使其更复杂?好吧,您可以先提示用户。

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    if Inputbox("If you are sure you want to stop the procedure, please type the " & _
                                                    "word 'STOP'") = "STOP" then
        Cancel = False

    Else

        Cancel = True

    End If

End Sub