Reputation: 19
I have some Data in my Csv. file and I need to delete all rows except every 5th, how can I do that ?
Upvotes: 0
Views: 94
Reputation: 1343
You could make a helper column with this formula:
=MOD(ROW(A2)-ROW($A$2)+1;5)
Replace semicolon with comma, if your Excel version needs.
ROW($A$2) is the first row of the data.
Then apply a filter Data--> Filter
and remove the tick for the 0
Then delete rows.
then remove the filter
Delete sheet rows
Remove Filter
Remove helpercolumn
Then you can export the Excel file as csv. Opening a csv in Excel, changing it and saving, might not work.
Upvotes: 0
Reputation: 76000
I'd advise you to load the CSV into PowerQuery. Though PQ by no means is my forte, I'd then take the following steps:
=Number.Mod([Index],5)=0
;For example:
For what it's worth, this is the m-code of me loading the data from my worksheet (source). You can load the data through CSV:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Number.Mod([Index],5)=0),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index", "Custom"})
in
#"Removed Columns"
Upvotes: 3
Reputation: 17575
Not-tested pseudo-code, but it will be something like that:
for i=end downto beginning:
if mod(i,5) != 0 then
Range(i,1).EntireRow.Delete
end if
step -1
It is crucial to go from end to beginning, or you'll mess up the indexes in your rows :-)
Upvotes: 0