选择中的VBA Rows.Count

时间:2017-08-11 09:46:05

标签: vba excel-vba excel

我正在研究用户选择在行动按钮旁边的工作表顶部显示的行数,即。按钮显示“生成电子邮件”,旁边显示“已选择x项”。

每次更改选择时都会更新,我有以下代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Sheet1.Range("E1") = Target.Rows.Count & " items selected"
End Sub

如果用户选择连续的行,这可以正常工作,例如7:10返回4.

我的问题是用户是否选择了第7行和第10行。它只返回1(选择的第一部分中的行)。

根据我的发现,没有办法从属性中获取此值,但我无法理解如何遍历选择/目标的所有部分并计算行的总和。然后还有用户选择说A7,C7和A10的可能性。 A7和C7涉及同一个项目,所以这应该只被视为一个,而不是两个,我认为我的假设代码会做...

有没有人试图在此之前实现这一目标并取得成功,还是可以指向一些可能有帮助的属性方向?我尝试了一个单独的功能来实现它,但这也没有用。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Sheet1.Range("E1") = getRowCount(Target) & " items selected"
End Sub

Function getRowCount(selectedRanges As Ranges)
  rowCount = 0
  For Each subRange In selectedRanges
    rowCount = rowCount + subRange.Rows.Count
  Next

  getRowCount = rowCount
End Function

5 个答案:

答案 0 :(得分:2)

我认为这会奏效。 (当我尝试它的时候。)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Create a range containing just column A
    Dim subRange As Range
    Dim r As Range
    For Each subRange In Target.Areas
        If r Is Nothing Then
            Set r = subRange.EntireRow.Columns(1)
        Else
            Set r = Union(r, subRange.EntireRow.Columns(1))
        End If
    Next
    'Count how many cells in the combined column A range
    Sheet1.Range("E1") = r.Cells.Count & " items selected"
End Sub

答案 1 :(得分:1)

Sub NumberOfRowsSelected()
Dim vMatch As Variant, aRows() As Long, r As Range, x As Long

ReDim Preserve aRows(x)
aRows(x) = 0

For Each r In Selection.Cells
    vMatch = Application.Match(r.Row, aRows, 0)

    If IsError(vMatch) Then
        x = x + 1
        ReDim Preserve aRows(0 To x)
        aRows(x) = r.Row
    End If
Next r

MsgBox UBound(aRows)

End Sub

修改后的代码转换为函数

Sub NumberOfRowsSelected()
    MsgBox RowsCount(Selection)
End Sub

Function RowsCount(rRange As Range) As Long
Dim vMatch As Variant, aRows() As Long, r As Range, x As Long

ReDim Preserve aRows(x)
aRows(x) = 0

For Each r In rRange.Cells
    vMatch = Application.Match(r.Row, aRows, 0)

    If IsError(vMatch) Then
        x = x + 1
        ReDim Preserve aRows(0 To x)
        aRows(x) = r.Row
    End If
Next r

RowsCount = UBound(aRows)

End Function

答案 2 :(得分:1)

另一种方法,构建一串已检查的行似乎很简单,以避免重复计算。有关详细信息,请参阅注释:

Function getRowCount(rng As Range) As Long
    Dim c As Range
    ' Keep track of which rows we've already counted
    Dim countedrows As String: countedrows = ","
    ' Loop over cells in range
    For Each c In rng
        ' Check if already counted
        If Not InStr(countedrows, "," & c.Row & ",") > 0 Then
            ' Add to counted list
            countedrows = countedrows & c.Row & ","
        End If
    Next c
    ' Get number of rows counted
    Dim rowsarr() As String: rowsarr = Split(countedrows, ",")
    getRowCount = UBound(rowsarr) - LBound(rowsarr) - 1
End Function

答案 3 :(得分:0)

您需要计算用户选择的每个List<XML_Handler.PrUnits>中的行数 https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-areas-property-excel

<ListView x:Name="listUnits" ItemsSource="{Binding Units}">
...

答案 4 :(得分:0)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cell As Range
Dim i, currentRow As Long: i = 0
'get row of first cell in range
currentRow = Target.Cells(1, 1).row

For Each cell In Target
    'if row is different, then increase number of items, as it's next item
    If Not currentRow = cell.row Then
       i = i + 1
       currentRow = cell.row
    End If
Next cell

Range("E1").Value = i

End Sub