VBA 사용중 가장 자주쓰는 부분을 올려놓는다. 필요할때 수정해서 사용하기 위해...
dim c as range
dim firstAddress as string
With Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
dim firstAddress as string
With Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
Set c = .Find(2, lookin:=xlValues)
With
에서 설정한 영역에서 2를(what:=2
를 줄여쓴 것임) 찾는데, lookin:=xlValues
: 값에서 찾는다.lookin
옵션에는 xlValues, xlFormulas
가 있다.이 예제에서는 생략됐지만
lookat
라는 속성이 있으며, 해당 옵션에는 xlWhole, xlPart
가 있다.If Not c Is Nothing Then
만약, 2를 찾았다면,
End if
를 만날때까지 영역을 실행한다. 그렇지 않다면 End If 다음 줄로 넘어간다.
c.Value = 5
찾은 2의 값을 5로 변경한다.
Set c = .FindNext(c)
영역에서 2를 더 찾아서 c에 저장한다.
Loop While Not c Is Nothing And c.Address <> firstAddress
c 가 비어있지 않고, c의 주소가 처음에 찾은 위치가 아니라면 계속해서
Do ~ Loop
사이를 실행하게 한다.
이 예제는 선택 범위에서 2를 찾아서 5로 변경해주는 예제인데, 검색해서 처리하는 업무에 응용하여 활용하기 아주 좋다.
댓글 없음:
댓글 쓰기