1.在Excel中进入Visual Basic 编辑界面 1. 选择插入用户窗体 2. 生成用户窗体UserForm1 3. 设置窗体UserForm1 的hieght(高)为28,width(宽)为570 就变成了下图: 设置UserForm的 BackColor属性为&FFFFFF&,在窗体中加入1个标签Label1 ,设置Label1 的Caption属性为空,BackColor属性为&H0080FF8080&(蓝色),left属性为0,再加入label2 设置属性caption为空,Backstyle为0-fmBackstyleTransparent, 双击进度条窗体进入代码界面 代码界面: 编写代码: 代码如下: OptionExplicit PrivateDeclare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) AsLong PrivateDeclare Function GetWindowLong Lib "user32" Alias"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long PrivateDeclare Function SetWindowLong Lib "user32" Alias"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByValdwNewLong As Long) As Long PrivateDeclare Function FindWindow Lib "user32" Alias"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName AsString) As Long PrivateConst GWL_STYLE As Long = (-16) PrivateConst GWL_EXSTYLE = (-20) PrivateConst WS_CAPTION As Long = &HC00000 PrivateSub UserForm_Activate() Dim m_循环次数 As Long Dim i As Long m_循环次数 = 100000 '根据实际应用视情况再定循环次数() Me.Label2.Left = Me.Width / 2 '标签2的位置在窗体正中 Me.Label1.Height = Me.Height '标签1的高度等于窗体高度 With 进度条 .Show 0 '显示窗体 For i = 1 To m_循环次数 .Label1.Width = Int(i / m_循环次数 *Me.Width) '改变标签宽度 .Label2.Caption = CStr(Int(i / m_循环次数 * 100))+ "%" '更改标签的名称,用以显示进度条百分比 DoEvents '进度条更清晰 Next i .Hide '进度条循环完成后就隐藏 End With End Sub PrivateSub UserForm_Initialize() Dim iStyle As Long Dim hWnd As Long If Val(Application.Version) < 9 Then hWnd =FindWindow("ThunderXFrame", Me.Caption) Else hWnd =FindWindow("ThunderDFrame", Me.Caption) End If iStyle = GetWindowLong(hWnd, GWL_STYLE) iStyle = iStyle And Not WS_CAPTION SetWindowLong hWnd, GWL_STYLE, iStyle DrawMenuBar hWnd '进度条.Height = 15 End Sub 返回到Excel工作界面,在界面中插入一个命令按钮 双击命令按钮进入VBA编程界面 写入: 进度条.Show 0 Ok 大功告成.( NND口水话一大堆。)
|