vba 我可以将自己的功能添加到 Excel 中的自定义状态栏吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18612262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:08:24  来源:igfitidea点击:

Can I add my own function to the customized status bar in Excel?

excel-vbaexcel-2010statusbarvbaexcel

提问by Lewistrick

If I select a number of cells in Excel, I can see some functions on those in the Customized status bar. This information is my own selection out of the following:

如果我在 Excel 中选择多个单元格,我可以在自定义状态栏中看到一些功能。此信息是我自己从以下信息中选择的:

  • Average
  • Count
  • Numerical count
  • Minimum
  • Maximum
  • Sum
  • 平均数
  • 数数
  • 数字计数
  • 最低限度
  • 最大值

I want to add to this list a function that calculates the ratio of non-blank cells. The function would be =COUNTA(range) / (COUNTA(range) + COUNTBLANK(range)), but how can I get that into the status bar? I don't have much experience in VBA, so some explanation would be welcome.

我想在这个列表中添加一个计算非空白单元格比率的函数。该函数将是 =COUNTA(range) / (COUNTA(range) + COUNTBLANK(range)),但如何将其放入状态栏中?我在 VBA 方面没有太多经验,因此欢迎提供一些解释。

回答by Siddharth Rout

Try something like this (Not the best way to do it though but solves your purpose)

尝试这样的事情(虽然不是最好的方法,但可以解决您的目的)

Explanation: What this code does is checks if the user has selected a valid range (of minimum 2 cells) and then uses the Application.Evaluateto calculate your formula and then display it in the status bar. Also note that I have not done any error handling. I am sure you will take care of it :)

说明:此代码的作用是检查用户是否选择了有效范围(最少 2 个单元格),然后使用Application.Evaluate计算公式,然后将其显示在状态栏中。另请注意,我没有进行任何错误处理。我相信你会照顾它:)

Paste this in the relevant sheet code area.

将此粘贴到相关的工作表代码区域。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If TypeName(Target) = "Range" Then
        If Target.Cells.Count > 1 Then
            Application.StatusBar = "My Function: " & _
                                    Application.Evaluate( _
                                                          "=COUNTA(" & _
                                                           Target.Address & _
                                                           ") / (COUNTA(" & _
                                                           Target.Address & _
                                                           ") + COUNTBLANK(" & _
                                                           Target.Address & _
                                                           "))" _
                                                           )
        End If
    End If
End Sub

This is a very basic way of doing it. In case you want it applicable for all sheets then you will have to amend it accordingly.

这是一种非常基本的方法。如果您希望它适用于所有工作表,则必须相应地对其进行修改。

Also the problem with this method is that it will suppress the application level messages in the status bar.

此方法的另一个问题是它会抑制状态栏中的应用程序级别消息。

Screenshot

截屏

enter image description here

在此处输入图片说明