programing

VBA에서 양식이 닫힐 때 코드 실행(Excel 2007)

lastcode 2023. 6. 11. 10:41
반응형

VBA에서 양식이 닫힐 때 코드 실행(Excel 2007)

사용자가 창 오른쪽 상단 모서리의 x 버튼을 사용하여 양식을 닫을 때 코드를 실행하고 싶습니다(Excel 스프레드시트를 열 때 양식 로드가 있고 Excel이 숨겨집니다).양식이 닫히면 Excel을 종료하거나 사용자가 수동으로 종료할 수 있도록 Excel을 다시 표시합니다.)

양식 속성을 보니 Unload 속성이 없고 양식이 닫혔을 때 실행되는 함수를 만드는 방법도 알 수 없습니다.

안타깝게도 VB로 코딩하는 것은 옵션이 아닙니다. VBA여야 합니다.

언로드 이벤트에 연결하는 방법이 아니라 엑셀을 숨기거나 완전히 종료하는 데 필요한 코드를 알고 있습니다.

다음과 같이 사용자 양식의 QueryClose 이벤트를 사용할 수 있습니다.

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        ' Your codes
        ' Tip: If you want to prevent closing UserForm by Close (×) button in the right-top corner of the UserForm, just uncomment the following line:
        ' Cancel = True
    End If
End Sub

다음과 같이 vbFormControlMenu를 사용할 수도 있습니다.

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        'Your code goes here
    End If
End Sub

동료가 다른 모든 사람을 위한 예를 포함하여 답을 제공할 수 있었습니다.

Private Sub userform_terminate()

    'Code goes here

End Sub

사용할 수 있습니다.Unload MeVBA로 입력하여 양식을 닫습니다.코드를 넣으면 바로 엑셀을 닫을 수 있습니다.

Private Sub Form_Unload(Cancel As Integer)
    Dim msgRes As VbMsgBoxResult
    msgRes = MsgBox("Exit form ?", vbYesNo)
    If msgRes = vbYes Then
        'optional code
    ElseIf msgRes = vbNo Then
        Cancel = True
    End If
End Sub

다음을 사용하여 X 단추를 클릭하면 양식이 닫히는 것을 방지할 수 있었습니다.

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
   Cancel = MsgBox("Please confirm cancellation", vbOKCancel + vbQuestion) = vbCancel
End Sub

다음과 같은 것을 시도해 보세요:-

Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _ 
    Handles Form1.FormClosing

    //Code you want to execute

End Sub

언급URL : https://stackoverflow.com/questions/3511903/execute-code-when-form-is-closed-in-vba-excel-2007

반응형