2017-01-23 338 views
0

我一直在寫這個宏,它有三個步驟。 首先是刪除行,如果行是C列步驟之後空在那裏與冠軍有留在工作簿就像捐款,所有其他課程費用行-Youth第三步步驟是通過在某些標題之後添加空格或空行來格式化該行。VBA:如何刪除行並根據條件保留一些行?

這是我的代碼,它似乎沒有編譯,我不知道如何防止行刪除...請幫助。

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 
+1

首先,'rw.Cells(n,3).Value =「貢獻 - 所有其他」和rw.Cells(n,3).Value =「節目費用 - 青春」將評估爲「False ' - 'rw.Cells(n,3).Value'不能等於'「貢獻 - 所有其他」** **和**等於'「節目費 - 青年」'。你需要使用'Or'而不是'And'。 – YowE3K

+1

而且我建議'Set rw = ActiveWorkbook.ActiveSheet.UsedRange.Rows'應該是'Set rw = ActiveWorkbook.ActiveSheet.UsedRange'。 – YowE3K

+0

除了這些評論,你能告訴我們你收到了什麼錯誤信息 - 這將使我們能夠找到編譯錯誤的原因。 – YowE3K

回答

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 

PS 「財務援助」應該是「財務援助」嗎?

+0

這些案件必須按規定? – MTBthePRO

+0

@MTBthePRO - 你的意思是「按順序」?它將執行第一個'Case'的語句,該語句的計算結果爲'True',因此從這個角度來看,它們需要按照您希望檢查語句的順序進行,但是您有三個(?)互斥事件,所以它對你來說並不重要(除了'Case Else'需要最後)。 – YowE3K

+0

我想通了。其中一個案例陳述缺少逗號。 – MTBthePRO

相關問題