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

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

这是我的代码,它似乎并没有编译,我不知道如何防止行删除…请帮助。

Sub RemoveRowsAndFormat() Dim WS As Worksheet For Each WS In Sheets WS.Activate Dim n As Long Dim nlast As Long Dim rw As Range Set rw = ActiveWorkbook.ActiveSheet.UsedRange.Rows nlast = rw.Count For n = nlast To 9 Step -1 If (rw.Cells(n, 3).Value = "Contributions-All Other" Or rw.Cells(n, 3).Value = "Program Fees - Youth" Or rw.Cells(n, 3).Value = "Financial Assitance" Or rw.Cells(n, 3).Value = "Salaries & Wages" Or rw.Cells(n, 3).Value = "Payroll Taxes" Or rw.Cells(n, 3).Value = "Employee Benefits" Or rw.Cells(n, 3).Value = "Staff Training and Confer." Or rw.Cells(n, 3).Value = "Occupancy" Or rw.Cells(n, 3).Value = "Supplies" Or rw.Cells(n, 3).Value = "Telephone" Or rw.Cells(n, 3).Value = "Postage & Shipping" Or rw.Cells(n, 3).Value = "Promotion and Advertising" Or rw.Cells(n, 3).Value = "Bad Debt" Or rw.Cells(n, 3).Value = "Program Operating Expense" Or rw.Cells(n, 3).Value = "Program Operating Net") Then rw.Rows(n).EntireRow.Insert ElseIf (rw.Cells(n, 4).Value = "" And rw.Cells(n, 5).Value = "" And rw.Cells(n, 6).Value = "" And rw.Cells(n, 7).Value = "" And rw.Cells(n, 8).Value = "" And rw.Cells(n, 9).Value = "" And rw.Cells(n, 10).Value = "" And rw.Cells(n, 11).Value = "") Then rw.Rows(n).Delete End If Next n Next WS End Sub 

您(编辑)的问题中的代码似乎是做你想做的,除了它是你的标题上面添加一行而不是之下 。 这可以通过更改rw.Rows(n).EntireRow.Insertrw.Rows(n + 1).EntireRow.Insert但这可能会导致问题(如果标题存在于最后一行)由于你的方式已经定义了rw

我已经重构了您的代码,以使用Select Case语句来replace您的(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语句可以很容易地包含如下function:

  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 

PS“财务援助”是“财务援助”吗?