I am trying to use some code from here. I have checked in some programmings forums and it seems to be ok. But I am getting a 438 error on showing an Userform used as progress bar. I don´t have any knowledge at all on how to use libraries (It is my first time).
The Header Code:
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
Procedure to hide the Title bar
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
When I try to show the Userform I got a constant 438 error on line: lFrmHdl = FindWindowA(vbNullString, frm.Caption)
. I did not find why.
The Macro I have created to call this Userform:
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
Any help will be appreciated.
UPDATE 1: I forgot to attach the code in the Userform:
Private Sub UserForm_Initialize()
Me.Height = Me.Height - 10
HideTitleBar.HideTitleBar Me
End Sub
If you are just wanting to prevent a user from clicking the X
close button, then you don't need to jump through a bunch of hoops to hide the title bar. Simply capture the event and cancel the request.
In your userform code module, paste this code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = True
End Sub
And that's all there is to it.
Want to make it more sophisticated? Well, you can prompt the user first.
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