source

셀이 변경될 때마다 Excel VBA가 매크로를 자동으로 실행

itover 2023. 4. 12. 22:17
반응형

셀이 변경될 때마다 Excel VBA가 매크로를 자동으로 실행

셀이 변경될 때마다 Excel이 자동으로 매크로를 실행할 수 있는 간단한 방법이 있습니까?

문제가 되는 세포는Worksheet("BigBoard").Range("D2")

구글의 단순한 조사라고 생각한 것은, 보다 복잡한 것으로 판명되고 있습니다.관련된 모든 샘플은 (그것이 무엇이든) 교차하거나 컬러 포맷, 또는 관련이 없는 것으로 보이는 다른 몇 가지 것들이 있습니다.

예, 워크시트 이벤트를 사용하면 가능합니다.

Visual Basic Editor에서 왼쪽 상단에 있는 트리의 워크시트 이름을 두 번 클릭하여 원하는 워크시트(예: "BigBoard")를 엽니다.모듈에 다음 코드를 입력합니다.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Me.Range("D2")) Is Nothing Then Exit Sub
    Application.EnableEvents = False 'to prevent endless loop
    On Error Goto Finalize 'to re-enable the events      
    MsgBox "You changed THE CELL!"
Finalize:        
    Application.EnableEvents = True
End Sub

또 다른 옵션은

Private Sub Worksheet_Change(ByVal Target As Range)
    IF Target.Address = "$D$2" Then
        MsgBox("Cell D2 Has Changed.")
    End If
End Sub

이 경우 사용하는 리소스가Intersect워크시트가 많이 바뀌면 도움이 됩니다.

교차 방식의 대상 셀을 이름 테이블 배열로 만드는 방법을 찾기 위해 특정 시트의 셀 또는 셀 세트가 변경될 때 무언가를 실행하는 간단한 방법을 우연히 발견했습니다.이 코드는 워크시트 모듈에도 배치됩니다.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 0 Then
'mycode here
end if
end sub

특정 컬럼(여기서는 "W", 즉 "23")의 어딘가에서 변화를 발견하기 위해 Peter Alberts의 답변을 다음과 같이 수정했습니다.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Target.Column = 23 Then Exit Sub
    Application.EnableEvents = False             'to prevent endless loop
    On Error GoTo Finalize                       'to re-enable the events
    MsgBox "You changed a cell in column W, row " & Target.Row
    MsgBox "You changed it to: " & Target.Value
Finalize:
    Application.EnableEvents = True
End Sub

사용자가 입력한 주소로 특정 셀 그룹에 이메일을 보내기 위해 다른 매크로에서 사용하는 이메일 주소를 입력하는 폼을 만들고 있었습니다.여러 사이트에서 이 간단한 코드를 패치하여 VBA에 대한 제한된 지식을 확보했습니다.이것은 단순히 하나의 셀(여기서는 K22)이 업데이트되기를 감시하고 그 셀의 하이퍼링크를 삭제합니다.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("K22")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        Range("K22").Select
        Selection.Hyperlinks.Delete

    End If 
End Sub

언급URL : https://stackoverflow.com/questions/15337008/excel-vba-run-macro-automatically-whenever-a-cell-is-changed

반응형