保留某些行并删除某些行

时间:2018-08-08 17:42:52

标签: excel matlab

我将几千行数据导入Excel,因此一个数据代表一行。我要保留行号。 1,11,21,31,....并删除第2至10、12至20、22至30 .....行,依此类推。请任何人帮助我。

4 个答案:

答案 0 :(得分:1)

这真的很好。它也超级快。只需将其粘贴到模块中即可。它将找到其中包含数据的最后一行,选择要删除的所有行,然后一次将其全部删除;)

Public Sub DeleteRows()

Dim rng As Range
Dim i As Long
Dim j As Long

Application.ScreenUpdating = False

i = Cells.Find("*", , xlFormulas, xlPart, xlRows, xlPrevious).Row

Set rng = Range("A2:A10")

For j = 11 To i Step 10
    Set rng = Union(rng, Range(Range("A1").Offset(j, 0), Range("A1").Offset(j + 8)))
    i = i - 10
Next

rng.Delete shift:=xlUp

Application.ScreenUpdating = True

End Sub

答案 1 :(得分:0)

应该这样做-假设您的A列具有最多数据,可以将lastRow设置为:

Sub remove_rows()
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

Dim i As Long
For i = lastRow To 2 Step -10
    Range(Cells(i - 1, 1), Cells(i - 9, 1)).Select
Next i
End Sub

请注意,删除行时,最好的做法是从 end 开始并向上移动。因此,Step -10

答案 2 :(得分:0)

也许是这样(如果您打算使用Matlab而不是VBA):

   % Suppose the variable "mydata" contains your data ^^
    nbrows = length(mydata);

    % id will contains only [1, 11, 21, ..., 1001, ...]
    id = nan(floor(nbrows/10), 1);
    zz = 1;
    for ii = 1:nbrows
       if mod(ii, 10) == 1
           id(zz, 1) = ii;
           zz = zz + 1;
       end
    end

    % results will only keep rows #1, 11, 21, 31, ... of mydata 
    results = mydata(id, 1);

欢呼

答案 3 :(得分:0)

如果导入到Matlab中,则无需删除行。您可以将所有内容替换为选定的行。例如,如果您导入一个名为Data的变量:

[r,c] = size (Data);  %So you know the number of rows.  Then you want every 10 rows begining with row 1, keeping all the columns
Data = Data(1:10:r, :);