每10行删除/隐藏9个?

时间:2017-09-29 15:22:36

标签: excel vba excel-vba

我找不到执行以下操作的方法:

假设我的数据集在5-50,000行数据输出之间(只有~20ish列)。我想对这个数据集进行图表处理,但它对内存非常重要。是否有一个宏可以隐藏每10行中的9个,从根本上将数据集减少到10%(更易于管理图形)?每隔几秒钟收集一次数据,这样我就可以轻松地删除多余的数据,而不会影响图表。

我已经尝试过"删除/隐藏其他所有行循环"但它仍然留下了大量的信息。我认为90%(或者,如果可调,X%切割)会有所帮助。感谢您的任何意见。

2 个答案:

答案 0 :(得分:1)

此代码将随机选择/删除90%的行。由于随机性,它可能不完全是90%。很多确保90%的方法但我更喜欢这种方法。

Sub RemoveRows()
    Dim inputRange As Range, removeRange As Range, r As Range

    Set inputRange = Sheet1.Range("A1:A1000")

    Randomize
    For Each r In inputRange
        If Rnd < 0.9 Then
            If removeRange Is Nothing Then
                Set removeRange = r
            Else
                Set removeRange = Union(removeRange, r)
            End If
        End If
    Next r

    removeRange.EntireRow.Select
    'removeRange.EntireRow.Delete
End Sub

答案 1 :(得分:0)

下面的代码将隐藏范围中的每个x行数。您可以在代码中为x变量rowCnt中指定=9的值。

Sub Demo()
    Dim rng As Range, cel As Range, hideRng As Range
    Dim lastRow As Long, rowCnt As Long, i As Long
    Dim ws As Worksheet

    rowCnt = 9                              'number of rows to hide
    Set ws = ThisWorkbook.Sheets("Sheet5")  'change Sheet5 to your data sheet

    With ws
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row with data using Column A

        For i = 2 To lastRow Step rowCnt + 1             'loop through Column A starting from Row 2
            If hideRng Is Nothing Then
                Set hideRng = Range(.Cells(i, 1), .Cells(i + rowCnt - 1, 1)) 'get range of 9 cells
            Else
                Set hideRng = Union(hideRng, Range(.Cells(i, 1), .Cells(i + rowCnt - 1, 1))) 'union range of 9 cells
            End If
        Next i
    End With

    hideRng.EntireRow.Select            'use this line to select rows
    'hideRng.EntireRow.Hidden = True    'use this line to hide rows
    'hideRng.EntireRow.Delete           'use this line to delete rows
End Sub