运行Excelmacros时显示完成百分比

我在macros中使用了一个“Worksheet_Change”事件,每次用户在下拉菜单中select一个选项时,需要大约15-20秒的时间才能运行,因为访问的数据范围较大。 我想在Excel中显示非常基本的%完成状态,以让用户知道它正在处理中。 使用默认的Excel Application.StatusBar就足够了,但是在我的工作簿中不可见。

我的工作隐藏/显示列macros:

Private Sub Worksheet_Change(ByVal Target As Range) Dim R, V If Target.Address = ("$K$7") Then V = [K7].Value For Each R In Range("R3:GJU3") If IsError(R.Value) Then R.EntireColumn.Hidden = True Else R.EntireColumn.Hidden = R.Value <> V End If Next End If End Sub 

我发现下面的代码在一个单独的线程,但我不确定如何修改我的目的基于我的macros上面。

在VBA Excel中的进度条中,将以下代码授予@eykanal

 Option Explicit Sub StatusBar() Dim x As Integer Dim MyTimer As Double 'Change this loop as needed. For x = 1 To 250 'Dummy Loop here just to waste time. 'Replace this loop with your actual code. MyTimer = Timer Do Loop While Timer - MyTimer < 0.03 Application.StatusBar = "Progress: " & x & " of 250: " & Format(x / 250, "Percent") DoEvents Next x Application.StatusBar = False End Sub 

更新你的代码如下

 Private Sub Worksheet_Change(ByVal Target As Range) Dim R, V Dim x As Range Dim counter As Long counter = 1 If Target.Address = ("$K$7") Then V = [K7].Value Set x = Range("R3:GJU3") For Each R In Range("R3:GJU3") If IsError(R.Value) Then R.EntireColumn.Hidden = True Else R.EntireColumn.Hidden = R.Value <> V End If DoEvents Application.StatusBar = Format(counter / x.Columns.Count, "Percent") counter = counter + 1 Next R End If End Sub 

您可以使用用户窗体的ProgressBar,结束了类似的东西

在这里输入图像描述

我的进度条在我的用户窗体的下半部分。 循环时,您需要增加进度条的宽度(这里是一个蓝光button)

这样做的代码是基本的(树的简单规则)。