반응형
1. 문제
아래와 같이 셀을 클릭했을 때 수평과 수직으로 표시가 되는데, 인쇄할 때 제외하는 방법을 알아보고자 합니다.

조건부 서식 인쇄시 문제점.xlsm
0.02MB
먼저 범위 외 셀을 클릭하면 조건부 서식이 적용되지 않으므로 범위 외 셀을 클릭하고 인쇄하는 방법이 있습니다.

그것이 아니라 자동으로 조건부 서식이 제거되고 인쇄되는 것을 구현하려고 합니다.
두 가지가 있는데 하나씩 다뤄보겠습니다.
2. 해법 1
가. 실행 방법
'조건부 서식을 지우기' 매크로와 '조건부 서식 원래대로' 매크로를 만든 다음 인쇄 버튼 클릭 시 '조건부 서식을 지우기'에 인쇄 부분이 있으므로 두 개를 연달아서 실행하면 됩니다.

나. 코드
코드는 아래와 같습니다.
Option Explicit
Dim sht As Worksheet
Sub 조건부서식지우기()
Dim PrintArea As Range
Set sht = Worksheets(1)
' 시트 전체에서 규칙 지우기
sht.Cells.FormatConditions.Delete
Set PrintArea = sht.Range("a1", Range("A4").CurrentRegion)
PrintArea.PrintPreview
Call 조건부서식원래대로
End Sub
Sub 조건부서식원래대로()
Dim rng As Range
'조건부 서식 변수 지정
Dim cfRule As FormatCondition
Set sht = Worksheets(1)
'조건부 서식의 메소드가 Add뿐이 없어서 기존 조건부서식 모두 지워야 함
sht.Cells.FormatConditions.Delete
'조건부 서식 적용 대상 범위 지정
'application.inputbox로 입력 받을 수 있음
' Set rng = Application.InputBox("조건부 서식을 적용할 범위를 지정하세요.", Type:=8)
'수동으로 범위 지정
Set rng = sht.Range("A4").CurrentRegion
rng.Select
'조건부 서식의 수식 입력
Set cfRule = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=ROW()=CELL(""row"")") ' 조건을 설정하는데 여기서는 $A1이 1인 경우를 확인하고 있습니다.
'이유는 잘 모르겠는데, 주석처리하면 안됨.
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
'조건부 서식의 서식 지정
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
'조건부 서식 입력 반복
Set cfRule = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=COLUMN()=CELL(""col"")")
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
sht.Range("T10").Select
End Sub
다. 코드 설명
Dim sht As Worksheet
두 개의 서브 프로시저에서 모두 사용되기 때문에 밖으로 빼냈습니다.
' 시트 전체에서 규칙 지우기
sht.Cells.FormatConditions.Delete
'조건부 서식 변수 지정
Dim cfRule As FormatCondition
Set PrintArea = sht.Range("a1", Range("A4").CurrentRegion)
PrintArea.PrintPreview
인쇄 영역을 변수에 저장하고 인쇄 미리 보기를 합니다.
Call 조건부서식원래대로
조건부서식원래대로 서브 프로시저를 호출합니다.
'조건부 서식 변수 지정
Dim cfRule As FormatCondition
'조건부 서식의 수식 입력
Set cfRule = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=ROW()=CELL(""row"")")
'이유는 잘 모르겠는데, 주석처리하면 안됨.
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
'조건부 서식의 서식 지정(가로 줄)
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
'조건부 서식 입력 반복(세로 줄)
Set cfRule = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=COLUMN()=CELL(""col"")")
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
sht.Range("T10").Select
라. 실행 결과
아래와 같이 인쇄 미리 보기가 조건부 서식이 지정되어 있더라도

인쇄 버튼을 누르면 조건부 서식 제거된 채로 인쇄되고,

인쇄가 끝나면 원래대로 돌아옵니다.

조건부 서식 인쇄시 문제점(해법1).xlsm
0.02MB
반응형
'EXCEL - VBA' 카테고리의 다른 글
차트의 마커(표식) 크기와 색상 변경하기 (0) | 2024.05.28 |
---|---|
소재지, 특지구분, 본번, 부번 합치기(2) - VBA (0) | 2024.05.23 |
VBA로 ColorIndex에 대한 색 표시하기 (0) | 2024.04.12 |
한글이 아닌 엑셀로 하는 메일 머지(3) (0) | 2024.03.29 |
한글이 아닌 엑셀로 하는 메일 머지(2) (2) | 2024.03.28 |