Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 5928|回复: 14

vba子窗口中如何结束父窗口的过程

[复制链接]
发表于 2011-7-4 14:57 | 显示全部楼层 |阅读模式
父窗口执行一个过程的时候会弹出子窗口(实际应用中子窗口是一个进度条),子窗口上有一个"取消"按钮,过秤运行结束后子窗口会自动消失回到父窗口的,请问的是如何当点击这个按钮后,自动让父窗口的这个过程停止。

期望是代码写在子窗口中而不是父窗口,因为子窗口(进度条)会在很多地方调用到,因此写在子窗口中只需要写一次,有利于代码的简化。

求助,谢谢~~

发表于 2011-7-4 19:12 | 显示全部楼层
回复

使用道具 举报

发表于 2011-7-4 19:13 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2011-7-5 10:45 | 显示全部楼层
本帖最后由 alex15 于 2011-7-5 11:25 编辑

Copy of VBA .zip (198.63 KB, 下载次数: 21)
回复

使用道具 举报

发表于 2011-7-5 11:08 | 显示全部楼层
你本来就需要调用DoEvents进行控件权调用,多一个变量无伤大雅
PS:我还是没看到附件
回复

使用道具 举报

 楼主| 发表于 2011-7-5 11:25 | 显示全部楼层
来啦附件,见上
回复

使用道具 举报

发表于 2011-7-5 12:33 | 显示全部楼层
Private Sub CommandButton1_Click()
Dim li_rows_1 As Integer
Dim li_rows_2 As Integer
Dim li_row_1 As Integer   '计数变量
Dim li_row_2 As Integer   '计数变量
Dim aaa1() As String        '用变量充当数组上下限要用Redim语句重新声明数组
Dim aaa2() As String
Dim li_columns As Integer
Dim a1, a2, b1, b2 As Integer

li_rows_1 = Sheets("Reference").Range("A" & Sheets("Reference").Rows.Count).End(xlUp).Row
li_rows_2 = Sheets("To Compare").Range("A" & Sheets("To Compare").Rows.Count).End(xlUp).Row
li_columns = Sheets("To Compare").Range("A1").End(xlToRight).Column

ReDim aaa1(li_rows_1, li_columns) As String
ReDim aaa2(li_rows_2, li_columns) As String

w_main.Hide
w_progressbar.Show 0
For a1 = 2 To li_rows_1
    For b1 = 1 To li_columns
        If aaa Then GoTo CancelProcess
        aaa1(a1, b1) = Trim(Sheets("Reference").Cells(a1, b1).Value)
    Next
    Call progressbar("Loading Page1...", a1 / li_rows_1)
Next
'Unload w_progressbar
w_progressbar.Show 0
For a2 = 2 To li_rows_2
    For b2 = 1 To li_columns
        If aaa Then GoTo CancelProcess
        aaa2(a2, b2) = Trim(Sheets("To Compare").Cells(a2, b2).Value)
    Next
    Call progressbar("Loading Page2...", a2 / li_rows_2)
Next
'Unload w_progressbar

w_progressbar.Show 0
For a2 = 2 To li_rows_2
    For a1 = 2 To li_rows_1 'sheet1's row
        If aaa Then GoTo CancelProcess
        If aaa2(a2, 1) = aaa1(a1, 1) Then
                For b2 = 2 To li_columns
                    If aaa Then GoTo CancelProcess
                   If aaa2(a2, b2) <> aaa1(a1, b2) Then
                        
                        Sheets("To Compare").Cells(a2, b2).Interior.ColorIndex = 6
                   End If
                Next
            Exit For
        End If
        If a1 = li_rows_1 Then
            If aaa2(a2, 1) <> aaa1(a1, 1) Then
                Sheets("To Compare").Rows(a2).Interior.ColorIndex = 33
            End If
        End If
    Next
    Call progressbar("Proceeding...", a2 / li_rows_2)
Next
CancelProcess:
Unload w_progressbar
MsgBox ("Done")
w_main.Show 0
End Sub
回复

使用道具 举报

 楼主| 发表于 2011-7-6 17:54 | 显示全部楼层
sanbe 发表于 2011-7-5 11:08
你本来就需要调用DoEvents进行控件权调用,多一个变量无伤大雅
PS:我还是没看到附件

真不意思,请问能不能具体点。或者你改在那个excel里面传一下呀,本人菜鸟,感激不尽~

唯一期望的是主要代码写在子窗口中而不是父窗口,因为子窗口(进度条)会在很多地方调用到,因此写在子窗口中只需要写一次,有利于代码的简化。
回复

使用道具 举报

发表于 2011-7-6 19:05 | 显示全部楼层
alex15 发表于 2011-7-6 17:54
真不意思,请问能不能具体点。或者你改在那个excel里面传一下呀,本人菜鸟,感激不尽~

唯一期望的是主 ...

我看了你的附件,有点头晕
当点取消时,你需要结束当前循环还结束整个查询?
回复

使用道具 举报

发表于 2011-7-6 19:13 | 显示全部楼层
我就当你结束整个过程处理,你要结束循环,自行更改
先在模块中增加一个全局变量

  1. Public bolExitFor As Boolean
复制代码


然后在进度条窗体中赋值给变量,同时关闭窗体

  1. Private Sub CommandButton1_Click()
  2.    bolExitFor = True
  3.    Unload w_progressbar
  4. End Sub
复制代码


再在主窗体中加入判断代码

  1. If bolExitFor Then
  2.         bolExitFor = False
  3.         Exit Sub
  4. End If
复制代码


Copy of VBA .rar

86.88 KB, 下载次数: 12

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-30 19:35 , Processed in 0.540180 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表