VBA:如何删除行并根据条件保留一些行?

时间:2017-01-23 20:01:18

标签: excel vba excel-vba

我一直在写这个宏,它有三个步骤。 首先 是在列 C 之后行为空白并且 第二 时删除行步骤是否有标题必须保留在工作簿中的行,如贡献 - 所有其他计划费用 - 青年 第三 步骤是通过在特定标题后添加空格或空行来格式化行。

这是我的代码,它似乎没有编译,我不确定如何阻止行删除...请帮忙。

var load_checklist = function() {
    var $checklistTable = $('#checklist-table');
    var $row = $('#checklist-row').html();
    $.get(BASE_URL + '/uapi/get_checklist', function(o) {
        var template = Handlebars.compile($row);
        $checklistTable.append(template(o));
    }, 'json');
};

1 个答案:

答案 0 :(得分:1)

您(已编辑)问题中的代码似乎正在执行您想要的操作,只是在您的标题上添加 行而不是 。这可以通过将rw.Rows(n).EntireRow.Insert更改为rw.Rows(n + 1).EntireRow.Insert来解决,但由于您定义rw的方式,这可能会导致问题(如果最后一行存在标题)。

我重构了您的代码,使用Select Case语句替换您的(IMO)笨拙的If语句,并在决定执行插入/删除操作时引用工作表而不仅仅是某些行

Sub RemoveRowsAndFormat()  
    Dim WS As Worksheet
    Dim n As Long
    Dim nlast As Long
    Dim rw As Range
    Dim c As Long
    Dim allEmpty As Boolean
    For Each WS In Worksheets
        With WS
            nlast = .UsedRange.Rows(.UsedRange.Rows.Count).Row
            For n = nlast To 9 Step -1
                Select Case .Cells(n, 3).Value 

                    Case "Contributions-All Other", _
                         "Program Fees - Youth", _
                         "Financial Assitance", _
                         "Salaries & Wages", _
                         "Payroll Taxes", _
                         "Employee Benefits", _
                         "Staff Training and Confer.", _
                         "Occupancy", _
                         "Supplies", _
                         "Telephone", _
                         "Postage & Shipping", _
                         "Promotion and Advertising", _
                         "Bad Debt", _
                         "Program Operating Expense", _
                         "Program Operating Net"

                        .Rows(n + 1).EntireRow.Insert

                    Case Else

                        allEmpty = True
                        For c = 4 To 11
                            If .Cells(n, c).Value <> "" Then
                                allEmpty = False
                                Exit For
                            End If
                        Next
                        'The above could be replaced by a "COUNTA", but I like this way
                        If allEmpty Then
                            .Rows(n).Delete
                        End If
                End Select
            Next n
        End With
    Next WS
End Sub

你在最近的评论中说,新问题“并非所有标题都需要间距”。如果是这样,Select Case语句可以很容易地包含该功能,如下所示:

                Select Case .Cells(n, 3).Value 

                    'Do nothing for headings which we just want to leave alone
                    Case "Contributions-All Other", _
                         "Program Fees - Youth", _
                         "Financial Assitance", _
                         "Salaries & Wages", _
                         "Payroll Taxes", _
                         "Employee Benefits", _
                         "Staff Training and Confer.", _
                         "Occupancy", _
                         "Supplies"

                    'Process cases where an additional row needs to be inserted
                    Case "Telephone", _
                         "Postage & Shipping", _
                         "Promotion and Advertising", _
                         "Bad Debt", _
                         "Program Operating Expense", _
                         "Program Operating Net"

                        .Rows(n + 1).EntireRow.Insert

                    'For all the other rows, check whether it needs to be deleted
                    Case Else

                        allEmpty = True
                        '...

(显然,我刚刚编写了哪些标题应该在他们之后插入行,哪些不应该。)

Select Case语句只是编写以下If语句的简化(?)方式:

If .Cells(n, 3).Value = "Contributions-All Other" Or _
   .Cells(n, 3).Value = "Program Fees - Youth" Or _
   .Cells(n, 3).Value = "Financial Assitance" Or _
   .Cells(n, 3).Value = "Salaries & Wages" Or _
   .Cells(n, 3).Value = "Payroll Taxes" Or _
   .Cells(n, 3).Value = "Employee Benefits" Or _
   .Cells(n, 3).Value = "Staff Training and Confer." Or _
   .Cells(n, 3).Value = "Occupancy" Or _
   .Cells(n, 3).Value = "Supplies" Then

ElseIf .Cells(n, 3).Value = "Telephone" Or _
       .Cells(n, 3).Value = "Postage & Shipping" Or _
       .Cells(n, 3).Value = "Promotion and Advertising" Or _
       .Cells(n, 3).Value = "Bad Debt" Or _
       .Cells(n, 3).Value = "Program Operating Expense" Or _
       .Cells(n, 3).Value = "Program Operating Net" Then

    .Rows(n + 1).EntireRow.Insert

Else

    allEmpty = True
    '...
End If

P.S。 “财务资助”应该是“经济援助”吗?