2011-10-14 2 views
1

사용자가 수식을 수정할 수 없도록 보호하려는 템플릿 파일이 있습니다. 시트가 보호되어 있으므로 사용자가 행을 삽입 할 수 있도록 매크로를 작성했습니다. 또한 사용자가 행을 삭제할 수있는 매크로를 원하지만 사용자가 특정 중요 행을 삭제하지 못하도록하고 싶습니다 (예 : 합계 및 제목 확인).사용자가 해당 행에있는 셀의 내용을 기반으로 특정 행을 삭제하지 못하도록합니다.

이 목적을 위해 필자는 템플릿에서 열 L을 사용하여 삭제할 수없는 행을 식별했습니다. 이러한 행의 경우 열 L의 해당 행에 "keep"이라는 단어가 있습니다. 기본 삭제 매크로를 아래에 작성했지만 "keep"이라는 단어가있는 경우 선택한 범위의 열 L을 찾기 위해 수정해야합니다. rRangeExit Sub입니다.

* rRange에는 많은 인접한 행이 포함될 수 있으므로 해당 행 중 하나라도 테스트에 실패하면 매크로를 종료해야합니다.

Sub DeteteRows() 

Dim rRange As Range 
On Error Resume Next 
    Application.DisplayAlerts = False 
    Set rRange = Application.InputBox(Prompt:= _ 
      "Please use mouse to select a row to Delete.", _ 
       Title:="SPECIFY ROW TO DELETE", Type:=8) 
On Error GoTo 0 
    Application.DisplayAlerts = True 
    If rRange Is Nothing Then 

    Exit Sub 

    Else 

rRange.EntireRow.Delete 
Range("a1").Select 

MsgBox ("Row(s) Deteted") 
    End If 

End Sub 

답변

0

이것은 최선의 방법은 아니지만 아래에 있습니다. 마지막 부분에 삭제 부분을 추가하지 않았습니다. 그 부분을 처리 할 수 ​​있다고 생각했을 때

Sub DeteteRows() 
Dim rRange As Range 
Dim bKeepFound As Boolean 
bKeepFound = False 
On Error Resume Next 
Application.DisplayAlerts = False 
Set rRange = Application.InputBox(Prompt:= _ 
"Please use mouse to select a row to Delete.", _ 
Title:="SPECIFY ROW TO DELETE", Type:=8) 
On Error GoTo 0 
    Application.DisplayAlerts = True 
    If rRange Is Nothing Then 
     Exit Sub 
     'dont need the else statement cause you exit the sub if it fails 
    End If 

    For Each Row In rRange.Rows 
    Dim s 'variable to hold the array 
    s = Split(Row.Address, ":") 'split out the column and row 
     'remove the $ and convert to a number then check the cell value 
     If rRange.Cells(CInt(Replace(s(0), "$", "")), 12).Value = "keep" Then 
      bKeepFound = True 
     End If 
    Next Row 
    'check to see if a row was found to keep 
    If bKeepFound Then 
     Exit Sub 'row was found so exit sub 
    Else 
     'delete the rows in the range 
    End If 

End Sub 
관련 문제