VBA를 처음 사용합니다. 이것이 내가하는 일을하는 최선의 방법이 아니라는 것을 알고 있습니다. 그래서 개선을위한 아이디어와 일반적인 방법론에 대한 도움을 주시면 감사하겠습니다!Excel VBA 복수 조건부 서식
내가 원하는 세 가지 형식이 있습니다. 보다 작음,보다 큼 및 빈 셀 형식
거의 모든 코드가 각 행을 해당 행 (목표)의 끝에있는 값과 비교하여 검사합니다. 목표보다 크거나 같으면 녹색으로 바뀝니다. 덜 작 으면, 빨강. 목표 셀이 비어 있으면 모든 셀이 비어 있습니다 (서식은 흰색 배경, 검은 색 텍스트). 셀이 비어 있으면 셀이 비어 있습니다 (흰색 배경).
나는 5 개의 매크로를 모두, 적은, 큰, 같음, 빈 목표, 빈 셀 순서로 실행했습니다. 이것은 올바른 형식으로 멋진 엑셀을 만들었습니다.
목표 번호를 변경하면 해당 행의 셀이 각각 변경됩니다. 큰! 그러나 이전에 빈 목표에 새 번호를 추가하거나 목표 번호를 삭제하면 매크로를 다시 실행하여 서식을 가져와야합니다.
사이드 노트 : 내 코드의 경우, 내가 원했던 행 (Empty Cell 제외)을 기록한 다음 추가 행을 루프로 바꿨습니다.
매크로 다시 실행을 제거하는 방법에 대한 아이디어가 있으십니까? 이상적으로이 Excel 문서는 VBA에 대해 모르는 사람에게 전송됩니다.
Sub Greater()
Dim i As Integer
Dim myRange As String
Dim myComparison As String
For i = 4 To 97
myRange = "C" & i & ":BC" & i
myComparison = "=$BD$" & i
Range(myRange).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:=myComparison
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Next i
End Sub
X
Sub Lesser()
Dim i As Integer
Dim myRange As String
Dim myComparison As String
For i = 4 To 97
myRange = "C" & i & ":BC" & i
myComparison = "=$BD$" & i
Range(myRange).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:=myComparison
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Next i
End Sub
X
Sub Equal()
Dim i As Integer
Dim myRange As String
Dim myComparison As String
For i = 4 To 97
myRange = "C" & i & ":BC" & i
myComparison = "=$BD$" & i
Range(myRange).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=myComparison
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Next i
End Sub
X
Sub EmptyGoal()
Dim i As Integer
Dim myRange As String
Dim myComparison As String
For i = 4 To 97
myRange = "C" & i & ":BC" & i
Range(myRange).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:=IsEmpty(Cells(i, 56))
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Next i
End Sub
X
Sub EmptyCell()
Range("C4:BC97").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(C4))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
데이터를 보지 않고 무엇을하고 있는지 정확하게 이해하기는 어렵지만 환경 설정 순서에 따라 4 개 또는 5 개의 조건을 사용하여 * 전체 데이터 세트 *에 한 번만 서식을 적용 할 수 있다고 생각합니다. 행을 반복하거나 매크로를 작성할 필요가 없습니다. 수식 옵션을 사용하여 가능한 변경 사항을 통합하는 방식으로 규칙을 작성할 수 있습니다. –