简体   繁体   English

如何使用VBA计算工作表中的单元格数

[英]How to count number of cell in a worksheet using vba

im new to Vba, I have 5 different sheet named sheet 1 to 5, the 1st sheet has a Button and Label so i want to select everything in sheet 3 and when i press the Button i want it to show me the number of cells that i selected in a Label 我是Vba的新手,我有5个不同的工作表,分别是工作表1至5,第一个工作表有一个按钮和标签,所以我想选择工作表3中的所有内容,当我按下按钮时,我希望它向我显示我在标签中选择

Sub Button2_Click()

Dim rngCell As Range, arrArray() As Variant, i As Integer

ReDim arrArray(1 To Selection.Cells.Count)

i = 1
For Each rngCell In Selection

    arrArray(i) = rngCell.Value
    i = i + 1

Next

ActiveSheet.Shapes("Label 1").Select
Selection.Characters.Text = i


End Sub

I think this is much simpler than you think ... 我认为这比您想象的要简单得多...

Option Explicit

Sub CaptureSelectionCount()

' Keyboard Shortcut: Ctrl+Shift+E '-> adjust to make sure this doesn't overwrite an existing function in your workbook
Dim lngCnt as Long

lngCnt = ActiveSheet.Selection.Cells.Count

Sheets("Sheet1").Shapes("Label 1").TextFrame.Characters.Text = lngCnt

End Sub

This will do it but it's not a very elegant way of doing things - I can't see any alternatives though. 这样就可以了,但这不是一种非常优雅的处理方式-我看不到任何替代方法。 You need to utilise Events in order to capture the worksheet that was previously selected. 您需要利用事件来捕获先前选择的工作表。 Since the only way of determining the range of a selection on a worksheet is to Activate that worksheet, you have to turn off screen updating jump to the worksheet and then jump back to the original worksheet and turn screen updating back on. 由于确定工作表上的选择范围的唯一方法是激活该工作表,因此您必须关闭屏幕更新跳转到工作表,然后跳回到原始工作表并重新打开屏幕更新。

Put the following code in a new module: Option Explicit 将下面的代码放在一个新的模块中:显式选项

'Global variables (avoid using globals if you can)
Public wsLast As Worksheet
Public iSelectedCells As Integer

'Link this sub to your button
Public Sub CountCells()
    If Not wsLast Is Nothing Then
        Sheets("Sheet1").Shapes("Label 1").TextFrame.Characters.Text = "There " & IIf(iSelectedCells = 1, " is " & iSelectedCells & " cell selected ", " are " & iSelectedCells & " cells selected ") & "in " & wsLast.Name
    End If
End Sub

The following code needs to go in the 'ThisWorkbook' module of your spreadsheet: 以下代码需要进入电子表格的“ ThisWorkbook”模块中:

Option Explicit

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim wsThisWorksheet As Worksheet

    'Turn off events to avoid triggering a loop
    Application.EnableEvents = False

    'Set this worksheet so we can come back to it
    Set wsThisWorksheet = ActiveSheet

    'Record the deactivated sheet as a global variable
    Set wsLast = Sh

    'Turn off screen updating, go back to the last sheet, count the selection
    Application.ScreenUpdating = False
    wsLast.Activate
    iSelectedCells = Selection.Cells.Count

    'Then come back to the original and turn screen updating back on
    wsThisWorksheet.Activate
    Application.ScreenUpdating = True

    'Restore events
    Application.EnableEvents = True

    'Set the local variable to nothing
    Set wsThisWorksheet = Nothing
End Sub

You could further enhance the code by checking if you're deactivating the worksheet with the button and ignore it if it is. 您可以通过检查是否要通过按钮停用工作表,并忽略它来进一步增强代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM