2017-01-23 13 views
0

私は3つのステップを持つこのマクロを書いています。 まず行が列C後に空白になっていると第二ステップは、貢献、他のすべてのまたはプログラムの費用などのワークブックのままにする必要がタイトルの行があるの場合は行を削除することです-Youth第3のステップは、特定のタイトルの後に空白または空の行を追加することによって行をフォーマットすることです。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 = "プログラム料金 - Youth"は 'False ' - ' rw.Cells(n、3).Value'は '' Contributions-All Other ''**と同じであってはいけません。**' ** '**' Program Fees - Youth ''と同じです。 'And'ではなく' Or'を使う必要があります。 – YowE3K

+1

そして、私は 'Set rw = ActiveWorkbook.ActiveSheet.UsedRange.Rows'はたぶん' Set rw = ActiveWorkbook.ActiveSheet.UsedRange'であるべきだとお勧めします。 – YowE3K

+0

これらのコメントとは別に、どのエラーメッセージが表示されているのかを教えてください。これにより、コンパイルエラーの原因になることがあります。 – YowE3K

答えて

1

あなた(編集済み)問題のコードは、それが代わりに以下それを上記のあなたの見出し行を追加していることを除いて、あなたが望むものをやっているようです。これはrw.Rows(n).EntireRow.Insertrw.Rows(n + 1).EntireRow.Insertに変更することで修正できますが、rwを定義したために問題が発生する可能性があります(最後の行に見出しがある場合)。

ステートメントを使用して、(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 .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 .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それSelect Case文は次のIf文を書くだけの簡単な方法であります「財政援助」は「財政援助」でなければならないか?

+0

ケースは順調である必要がありますか? – MTBthePRO

+0

@MTBthePRO - 「順番に」とはどういう意味ですか?それは、 'True'と評価される最初の' Case'のステートメントを実行します。その観点から、ステートメントをチェックしたい順番にする必要がありますが、相互に排他的なイベントが3つある(?)ので、あなたのために本当に重要ではありません(それ以外の 'Case Else'は最後に来る必要があります)。 – YowE3K

+0

私はそれを理解しました。ケースステートメントの1つにカンマがありませんでした。 – MTBthePRO

関連する問題