2017-04-26 18 views

答えて

0

次のコードは、あなたが必要なものを行う必要があります。

Sub InsertLineAfterGrandTotal() 
    Dim WS   As Integer 
    Dim LastLine As Long 
    Dim LookingArea As Range 
    Dim FoundCell As Range 
    For WS = 1 To Application.Sheets.Count 
     Sheets(WS).Activate 
     On Error Resume Next ' To avoid error when all cells are empty 
     LastLine = Sheets(WS).Cells.Find("*", LookIn:=xlFormulas, _ 
      SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row 
     Set LookingArea = Sheets(WS).Range(Cells(1, 1), Cells(LastLine, 1)) 
     Set FoundCell = LookingArea.Find("Grand Total", LookIn:=xlFormulas, _ 
      LookAt:=xlWhole, SearchOrder:=xlByColumns) 
     If FoundCell Is Nothing Then 
      MsgBox "Grand Total not found in sheet " & Sheets(WS).Name & _ 
       ". Press Ok to continue", vbExclamation + vbOKOnly, "Not found" 
     Else 
      FoundCell.Offset(1, 0).EntireRow.Insert 
     End If 
    Next WS 
End Sub 

説明する:

1)Forそれは、ブック内のすべてのワークシート全体で行く作るループ。

2)LastLineは、名前が示すように、アクティブなワークシートの最後の行を取得します。

3)LookingAreaは、文字列Grand Totalが検索される範囲です。

4)FoundCellは、Grand Totalが見つかったセルを指します。見つからない場合は、新しい行がその上に挿入されていることがわかると、メッセージボックスが表示されます。

+0

シート内のすべてのセルが空の場合、実行時エラー91が返されます。これを避けるには、LastLineで始まる 'On Error Resume Next'節を挿入することができます。 – Tehscript

+0

コードが更新されました:) – PedroMVM

関連する問題