根据单元格值删除整行

时间:2017-03-16 19:12:17

标签: excel vba excel-vba

如果该行中的单元格相等,我无法弄清楚如何删除整行。

这就是我所写的,关于如何使其发挥作用的任何想法?

 fs.readFile('./public/uploads/sample_file.xlsx', function (err, data) {
        if (err) {
            res.status(500).send(err);
        } else {
            var entries = parsing_file(req, res);                
            if (entries.length > 0) {
                async.eachLimit(entries, 10, function(entrie, callback){                      
                    model.insert_in_db(entrie, function(rows) {callback()});
                },function(err){
                      if( err ) {
                         // One of the iterations produced an error.
                         // All processing will now stop.
                         console.log('A file failed to process');
                      } else {
                          console.log('All files have been processed 
                          successfully');
                          return res.redirect('/admin');
                      }
               })                
        }           
  });

3 个答案:

答案 0 :(得分:1)

这是我的解决方案。它汇总了行中的所有值,并在总和为0时删除它。

Sub deleting_empty_rows()

Dim rng, rrng, rcell As Range
Dim i, j As Long

Set rng = Range("A1:E1")
Set rng = Range(rng, rng.End(xlDown))

For j = rng.Rows.Count To 1 Step -1

    Set rrng = rng.Rows(j)

    i = 0

    For Each rcell In rrng.Cells
        i = i + rcell.Value
    Next rcell

    If i = 0 Then rrng.EntireRow.Delete

Next j

End Sub

答案 1 :(得分:1)

我对您的代码进行了一些小修改,我认为它现在可以完成您希望它执行的操作。

Dim rng As Range, rrng As Range
Set rng = Sheet1.Range("A1:E1")
Set rng = Range(rng, rng.End(xlDown))
For Each rrng In rng
If rrng.Value = 0 Then Sheet1.Rows(rrng.Row).Delete
Next rrng

答案 2 :(得分:0)

要删除空行,请使用反向循环...

示例

Option Explicit
Public Sub Example()
    Dim Row&, i&

    i = Cells.SpecialCells(xlCellTypeLastCell).Row

    For Row = i To 1 Step -1
        Debug.Print Row ' Immediate Window
        If Application.CountA(Rows(Row)) = 0 Then
            i = i - 1

            If IsEmpty(Cells(Row, 1)) Then
                Rows(Row).Delete
            End If
        End If
    Next
End Sub