【问题标题】:Count rows based on text in cellExcel 计数宏仅有时工作
【发布时间】:2022-10-18 14:09:41
【问题描述】:

基本上,我的任务是通过以下任务在 excel 上制作用户表单。我应该做到这一点,以便它能够通过点击一个按钮, - 对数千个单元格的数据进行排序(一次接近10k),正在计算的数据类型分类如下,要合并,不会合并,合并,部分合并或被其他EA替换。 -我的最终目标是能够创建一个表格,可以通过按下按钮在其中输入这些行的计数。 - 代码还必须能够跳过隐藏的行,同时还能够计算它们。

现在,这段代码允许我计算所有 5 种类型的数量,而它无法计算其余的,例如,包括隐藏和未隐藏的总行数、隐藏行的总数、总行数其中不包含 5 种数据类型中的任何一种。

提前致谢!

   Private Sub CommandButton5_Click()
    Columns("P:x").ColumnWidth = 27.5
   Columns("P:x").HorizontalAlignment = xlCenter
   Columns("p:x").VerticalAlignment = xlCenter
    Dim R As Long
    Dim L As Long
    Dim N As Long
    Dim P As Long
    Dim O As Long
    Dim A As Long
    Dim F As Long
    Dim G As Long
    Dim col As Range, i As Integer

    Dim c As Long
    Dim MyRange As Range
    Dim myCell As Range
    Dim M, range_1 As Range
    Set range_1 = Range("J1").EntireColumn
    With range_1
    R = Worksheets("Default").Cells(Rows.Count, "A").End(xlUp).Row
    


    For L = 2 To R
        If Worksheets("Default").Cells(L, "J") = "Incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        N = N + 1
        End If
        If Worksheets("Default").Cells(L, "J") = "To be incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        M = M + 1
        End If
        If Worksheets("Default").Cells(L, "J") = "Won't be incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        O = O + 1
        End If
        If Worksheets("Default").Cells(L, "J") = "Partially incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        P = P + 1
        End If
         If Worksheets("Default").Cells(L, "J") = "Replaced by other EA" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        G = G + 1
        End If
      
    Next
    End With
    Worksheets("default").Cells(R, "P") = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count
    A = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count
    Worksheets("Default").Cells(R, "s") = N
    Worksheets("Default").Cells(R, "R") = M
    Worksheets("Default").Cells(R, "U") = O
    Worksheets("Default").Cells(R, "q") = N + M + O + P + G
    Worksheets("Default").Cells(R, "T") = P
    Worksheets("Default").Cells(R, "V") = G
    Worksheets("Default").Cells(R, "w") = (ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count - (N + M + O + P + G)) - 1
    Worksheets("Default").Cells(R - 1, "s") = "EAs that are incorporated"
    Worksheets("Default").Cells(R - 1, "R") = "EAs that are To be Incorporated"
    Worksheets("Default").Cells(R - 1, "U") = "EAs that won't be Incorporated"
    Worksheets("Default").Cells(R - 1, "q") = "EAs with an incorporation status"
    Worksheets("Default").Cells(R - 1, "T") = "EAs with partially incorporated"
    Worksheets("Default").Cells(R - 1, "w") = "EAs without incorporation status"
    Worksheets("Default").Cells(R - 1, "P") = "Visible EAs"
    Worksheets("Default").Cells(R - 1, "x") = "Non-visible EAs"
    Worksheets("Default").Cells(R - 1, "V") = "EAs Replaced by other EA"
    Worksheets("Default").Cells(R, "x") = R - A - 1
    MsgBox A
    


End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这部分效率很低。

    For L = 2 To R
        If Worksheets("Default").Cells(L, "J") = "Incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        N = N + 1
        End If
        If Worksheets("Default").Cells(L, "J") = "To be incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        M = M + 1
        End If
        If Worksheets("Default").Cells(L, "J") = "Won't be incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        O = O + 1
        End If
        If Worksheets("Default").Cells(L, "J") = "Partially incorporated" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        P = P + 1
        End If
         If Worksheets("Default").Cells(L, "J") = "Replaced by other EA" And (Worksheets("Default").Rows(L).EntireRow.Hidden = False) Then
        G = G + 1
        End If    
    Next
    

    想象一下Cells(L, "J")Incorporated,如果你已经知道它们不可能,你为什么还要走得更远并检查它是否是所有其他的。你的代码只是浪费时间。

    For L = 2 To R
        If Worksheets("Default").Rows(L).EntireRow.Hidden = False Then
            Select Case Worksheets("Default").Cells(L, "J")
                Case "Incorporated":            N = N + 1
                Case "To be incorporated":      M = M + 1
                Case "Won't be incorporated":   O = O + 1
                Case "Partially incorporated":  P = P + 1
                Case "Replaced by other EA":    G = G + 1
            End Select
        End If
    Next L
    

    所以这将首先检查该行是否未被隐藏。然后才检查列J 的值。一旦Select Case 找到一个值,它就会停止检查其他值。


    对于您的计数问题:

    不要使用ActiveSheet 而是指定您所指的工作表名称。切勿让 Excel 猜测要查看哪个工作表,在代码执行期间单击鼠标可能会更改活动工作表。

    您计数的问题是以下

    ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count
    

    只会给你一个非连续范围的第一个区域的行数。
    如果您需要知道所有可见行的数量,您需要遍历所有区域并进行总结。

    或者作为一种解决方法,您可以执行以下操作:

    ActiveSheet.UsedRange.Resize(ColumnSize:=1).SpecialCells(xlCellTypeVisible).Cells.Count
    

    这里的技巧是使用Resize(ColumnSize:=1) 将使用范围限制为一列,并计算那里的可见单元格(与可见行的数量相同),但不同之处在于这是计算所有区域的总数!

    【讨论】:

    • 您好,是的,它肯定会加快代码速度。这次真是万分感谢。但是,我的 A 值似乎被打破了。我需要它来计算行中完全可见且未隐藏的所有单元格,因为其中一个单元格需要对不可见的单元格进行计数,我将公式设为(可见单元格的总和 - 的总和不可见的单元格)。你有什么解决办法吗?如果我根本没有隐藏任何单元格,则代码工作得非常好,但是当任何单元格被隐藏时,代码只是决定提交 seppuku 并给出错误的值。
    • @remy 看到我编辑的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多