|
* 답변하시는 분들께 도움이 되도록 자신의 환경을 아래 항목 옆에 기재해 주세요.
- 엑셀 버전(95,97,2000,xp,2003,2007): 2021
* 아래줄에 질문을 작성하세요 >>
숫자를 클릭하면 다른 영역에서 같은 값을 가진 셀이 하일라이트 되도록 VBA코드를 작성했습니다. 빈 곳을 제외하고 값을 가진 곳만 range설정이 되도록 했는데 매번 클릭할 때마다 영역을 설정하는 시간이 소요됩니다.
프로시져 외부에서(예를 들면 시트를 열 때에 한 번) 영역을 설정해 놓고 사용하면 좋은 데 잘 안되는군요.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ws As Worksheet
Dim cell As Range
Dim range1 As Range
Dim range2 As Range
Dim range3 As Range
Dim checkRange As Range
Dim targetRange As Range
Dim nonEmptyRange As Range
Set ws = ActiveSheet
Set targetRange = ws.Range("F6:Q27")
' 값이 있는 셀만 선택
For Each cell In targetRange
If Not IsEmpty(cell.Value) Then
If nonEmptyRange Is Nothing Then
Set nonEmptyRange = cell
Else
Set nonEmptyRange = Union(nonEmptyRange, cell)
End If
End If
Next cell
Set targetRange = nonEmptyRange
Set selectRange = Union(ws.Range("S8:S27"), ws.Range("W8:W27"), ws.Range("AA8:AA27"))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'[질문] selectRange와 targetRange를 파일을 열 때 한 번만 지정할 수 있는지
' 그래서 클릭 이벤트가 생기면 이 아래의 코드만 실행시킬 수 있는지
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set checkRange = Intersect(Target, selectRange)
' 범위내에서 클릭이 이루어졌는지
If Not checkRange Is Nothing Then
If Target.Count = 1 Then
If Target.Value <> 0 And Target.Value <> "" Then
For Each cell In targetRange
If cell.Value = Target.Value Then
cell.Interior.Color = RGB(255, 255, 0) ' 노란색으로 바탕
cell.Font.Bold = True '볼드설정
Else
cell.Interior.ColorIndex = xlNone ' 일치하지 않는 셀의 하이라이트 제거
cell.Font.Bold = False
End If
Next cell
End If
End If
End If
End Sub
==============[추레하니님 글에 대한 답변입니다]==============
첨부파일의 시트코드 참고하세요... |
|