【问题标题】:add the count of non-blank cells to the count of preceeding value VBA将非空白单元格的计数添加到先前值 VBA 的计数中
【发布时间】:2016-02-12 11:04:28
【问题描述】:

我是 excel vba 的新手,我需要帮助解决我面临的问题。

我在上面的链接中放了一个小例子。 我想要的是计算定义范围内每列中的字符数。 但是,如果有空白单元格,则空白单元格将计入前面的非空白值。 在示例 1 中:单元格 3,4 为空,其前面的非空白值为 R。因此 R 的计数变为 4。 单元格 6 也是空的,因此被添加到 Y 的计数中,即前面的非空值。所以 Y 的计数是 2。

在示例 2 中:单元格 1,2 是空的,但是它们没有任何前面的非空值,因此它们不被计算在内。 此外,单元格 4、5、6 是空的。但是,它们有一个前面的非空白值 Y。 所以 Y 的计数是 4

有人可以帮我用 VBA 编写代码吗?

【问题讨论】:

  • 会有一个包含行数据的列,还是只是显示它来表示电子表格中的行?
  • 嗨 Matthew,我只是展示它来表示电子表格中的行。
  • 确实 (a) 有必要保留空白并且您无法将列附加到数据中吗?这必须用VBA完成吗?如果这两个答案都不是,那么基本的 excel 功能可以在这里覆盖你。

标签: vba excel


【解决方案1】:

假设您在 A 列有行索引,B 列有数据,并且工作表中的数据从第 3 行开始(如图所示),我建议使用以下代码:

    Sub test()
        Dim rowNum As Integer
        Dim prevRowData As String
        Dim rCount, yCount

        rowNum = 3
        prevRowData = ""
        rCount = 0
        yCount = 0
        Do While Trim(Range("A" & rowNum).Value) <> ""
                Select Case (Trim(Range("B" & rowNum).Value))
                 Case "R"
                    rCount = rCount + 1
                    prevRowData = "R"
                 Case "Y"
                    yCount = yCount + 1
                    prevRowData = "Y"
                 Case ""
                    If prevRowData = "R" Then
                      rCount = rCount + 1
                    ElseIf prevRowData = "Y" Then
                      yCount = yCount + 1
                    End If
                End Select
                rowNum = rowNum + 1
        Loop

        Range("A" & (rowNum + 1)).Value = "Count of R:" & rCount
        Range("A" & (rowNum + 2)).Value = "Count of y:" & yCount
    End Sub

【讨论】:

  • 您好 Animesh,您的解决方案确实有效。它有一个小问题,但它纠正了它。两次都将 prevRowData 与“R”进行比较,但都是次要的。非常感谢你的帮助。它帮助我解决了我的问题。我能够使逻辑适应我的代码。
【解决方案2】:

这样的事情就可以了。需要注意的是,这只会转到使用的最后一行,因此您的第 6 行不会被计算在内,因为第 5 行之外没有数据。如果您知道该行,您可以将 lastRow 替换为要转到的行号,但我'不知道你怎么知道应该计算的最后一个空白行。

在您的 VBA IDE 中,转到工具菜单并选择引用。选择“Microstoft ActiveX 数据对象 2.8 库。

我们将使用 ADODB 记录集来存储我们找到的数据。

这假设您的列表在 Sheet1 上并且数据在 A 列中。它将在读取的数据下方写出摘要。

Private Sub CommandButton1_Click()
    Dim rs As New ADODB.Recordset
    Dim ws As Excel.Worksheet
    Set ws = ActiveWorkbook.Sheets("Sheet1")
    Dim lastRow As Long
    Dim lRow As Long
    Dim szLastData As String

    'Add fields to your recordset for storing data.
    With rs
        .Fields.Append "Value", adChar, 25
        .Fields.Append "Count", adInteger
        .Open
    End With

    'This is getting the last used row in column A
    lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row

    'Loop through the rows
    lRow = 1
    Do While lRow <= lastRow

        'Update the value we will search for in out rs if there is data in the current row
        If ws.Range("A" & lRow).Value <> "" Then
            szLastData = ws.Range("A" & lRow).Value
        End If

        'Check if this is already data that we are counting
        rs.Filter = ""
        rs.Filter = "Value='" & szLastData & "'"

        If rs.RecordCount = 0 Then
            'If this is new data, add a new row for it
            rs.AddNew
            rs.Fields("Value").Value = ws.Range("A" & lRow).Value
            rs.Fields("Count").Value = 1
            rs.Update
        Else
            'If we get here, we already have this data.
            'Increment the count by 1
            rs.Fields("Count").Value = rs.Fields("Count").Value + 1
            rs.Update
        End If

    lRow = lRow + 1
    Loop

    'Remove the filer and move to the first record in the rs
    rs.Filter = ""
    rs.MoveFirst

    'Move down a row
    lRow = lRow + 1

    'Loop through the data we found and write a summary
    Do While rs.EOF = False
        ws.Range("A" & lRow).Value = rs.Fields("Value").Value
        ws.Range("B" & lRow).Value = rs.Fields("Count").Value
        lRow = lRow + 1
    rs.MoveNext
    Loop

End sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多