엑셀 VBA에서 Sheets.Delete 메서드는 특정 시트를 삭제하는 기능을 제공합니다.
즉, 워크북 내에서 필요 없는 시트를 VBA 코드로 삭제할 때 사용됩니다.
1. 기본 문법
Sheets("시트이름").Delete
|
또는
Sheets(시트번호).Delete
|
- Sheets("시트이름") → 삭제할 시트의 이름 지정
- Sheets(시트번호) → 삭제할 시트의 번호 지정 (Sheets(1)은 첫 번째 시트)
📌 삭제 시 기본적으로 경고 메시지가 나타남
📌 Application.DisplayAlerts = False를 사용하면 경고 메시지 없이 자동 삭제됨
📌 삭제된 시트는 복구할 수 없음 (삭제 전에 백업 필요)
2. 특정 시트 삭제
Sub 특정시트_삭제()
Sheets("Sheet2").Delete
End Sub
|
📌 "Sheet2" 시트를 삭제
📌 기본적으로 삭제 경고 메시지가 표시됨
3. 삭제 경고 메시지 없이 시트 삭제
Sub 특정시트_삭제_경고없음()
Application.DisplayAlerts = False ' 경고 메시지 비활성화
Sheets("Sheet2").Delete
Application.DisplayAlerts = True ' 다시 활성화
End Sub
|
📌 "Sheet2"를 삭제하되, 경고 메시지를 표시하지 않음
4. 현재 활성화된 시트 삭제
Sub 현재시트_삭제()
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub
|
📌 현재 활성화된(선택된) 시트를 삭제
5. 첫 번째 시트 삭제
Sub 첫번째시트_삭제()
Application.DisplayAlerts = False
Sheets(1).Delete
Application.DisplayAlerts = True
End Sub
|
📌 첫 번째 시트를 삭제 (이름과 상관없이 위치 기준으로 삭제됨)
6. 마지막 시트 삭제
Sub 마지막시트_삭제()
Application.DisplayAlerts = False
Sheets(Sheets.Count).Delete
Application.DisplayAlerts = True
End Sub
|
📌 현재 마지막 시트를 삭제
7. 시트 개수 확인 후 삭제 (최소 1개 남기기)
Sub 시트개수_확인후삭제()
If Sheets.Count > 1 Then
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
Else
MsgBox "더 이상 삭제할 수 없습니다. 최소 1개의 시트는 유지해야 합니다.", vbExclamation
End If
End Sub
|
📌 시트 개수가 1개 이상일 경우에만 삭제
📌 마지막 남은 시트는 삭제되지 않도록 방지
8. 여러 개의 시트 삭제
Sub 여러시트_삭제()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Sheets
If ws.Name <> "Sheet1" Then ' 특정 시트 제외
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
End Sub
|
📌 "Sheet1"을 제외한 모든 시트를 삭제
9. 특정 단어가 포함된 시트 삭제
Sub 특정단어_포함된시트_삭제()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Sheets
If InStr(ws.Name, "보고서") > 0 Then ' "보고서"가 포함된 시트 삭제
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
End Sub
|
📌 이름에 "보고서"가 포함된 시트만 삭제
📌 예: "월별보고서", "연간보고서" 등의 시트가 삭제됨
10. 빈 시트만 삭제
Sub 빈시트_삭제()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Sheets
If WorksheetFunction.CountA(ws.UsedRange) = 0 Then ' 데이터가 없는 경우
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
End Sub
|
📌 데이터가 없는(완전히 빈) 시트만 삭제
11. 사용자가 선택한 시트 삭제 (InputBox 활용)
Sub 사용자입력_시트삭제()
Dim 시트이름 As String
시트이름 = InputBox("삭제할 시트의 이름을 입력하세요:", "시트 삭제")
If 시트이름 <> "" Then
On Error Resume Next ' 오류 방지
Sheets(시트이름).Delete
If Err.Number <> 0 Then
MsgBox "해당 이름의 시트가 존재하지 않습니다.", vbExclamation
End If
On Error GoTo 0
Else
MsgBox "삭제할 시트 이름을 입력하지 않았습니다.", vbExclamation
End If
End Sub
|
📌 사용자가 입력한 시트 이름을 삭제
📌 입력한 시트가 존재하지 않으면 오류 메시지 표시
12. 삭제 후 새로운 시트 추가
Sub 삭제후_새시트추가()
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
Application.DisplayAlerts = True
Sheets.Add.Name = "새로운시트"
End Sub
|
📌 "Sheet2"를 삭제한 후 새로운 시트 추가
📝 정리
기능
|
VBA 코드
|
특정 시트 삭제
|
Sheets("Sheet2").Delete
|
경고 메시지 없이 삭제
|
Application.DisplayAlerts = False: Sheets("Sheet2").Delete: Application.DisplayAlerts = True
|
현재 시트 삭제
|
ActiveSheet.Delete
|
첫 번째 시트 삭제
|
Sheets(1).Delete
|
마지막 시트 삭제
|
Sheets(Sheets.Count).Delete
|
시트 개수 확인 후 삭제
|
If Sheets.Count > 1 Then Sheets("Sheet1").Delete
|
여러 개 시트 삭제
|
For Each ws In Sheets: If ws.Name <> "Sheet1" Then ws.Delete
|
특정 단어 포함된 시트 삭제
|
If InStr(ws.Name, "보고서") > 0 Then ws.Delete
|
빈 시트만 삭제
|
If WorksheetFunction.CountA(ws.UsedRange) = 0 Then ws.Delete
|
사용자가 입력한 시트 삭제
|
Sheets(InputBox("삭제할 시트 이름?")).Delete
|
삭제 후 새 시트 추가
|
Sheets("Sheet2").Delete: Sheets.Add.Name = "새로운시트"
|
🔥 VBA의 Sheets.Delete 메서드를 활용하면 불필요한 시트를 손쉽게 삭제할 수 있습니다! 🚀