셀 변경시 자동으로 Excel 매크로 실행
특정 셀의 값이 변경 될 때마다 Excel 매크로를 자동으로 실행하려면 어떻게해야합니까?
지금 내 작업 코드는 다음과 같습니다.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H5")) Is Nothing Then Macro
End Sub
여기서 "H5"
모니터링되는 특정 셀 Macro
은 매크로의 이름입니다.
더 좋은 방법이 있습니까?
코드가 꽤 괜찮아 보입니다.
그러나에 대한 호출 Range("H5")
은에 대한 바로 가기 명령 Application.Range("H5")
이며 Application.ActiveSheet.Range("H5")
. 유일한 변경 사항이 사용자 변경 (가장 일반적인 경우) 인 경우 괜찮을 수 있지만 워크 시트의 셀 값이 프로그래밍 방식 변경 (예 : VBA)을 통해 활성 시트가 아닌 경우 변경 될 수 있습니다.
이를 염두에두고 다음을 활용합니다 Target.Worksheet.Range("H5")
.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("H5")) Is Nothing Then Macro
End Sub
또는 Me.Range("H5")
이벤트 처리기가 문제가되는 워크 시트의 코드 페이지에있는 경우 를 사용할 수 있습니다 (일반적으로 있음).
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("H5")) Is Nothing Then Macro
End Sub
도움이 되었기를 바랍니다...
Worksheet_Change
이벤트 또는 이벤트를 처리합니다 Workbook_SheetChange
.
이벤트 핸들러는 "Target As Range"인수를 사용하므로 변경되는 범위에 관심있는 셀이 포함되어 있는지 확인할 수 있습니다.
이 방법을 선호합니다. 셀이 아닌 범위를 사용합니다.
Dim cell_to_test As Range, cells_changed As Range
Set cells_changed = Target(1, 1)
Set cell_to_test = Range( RANGE_OF_CELLS_TO_DETECT )
If Not Intersect(cells_changed, cell_to_test) Is Nothing Then
Macro
End If
나는 이것을 조사하고 이벤트 트리거를 정말로 엉망으로 만든 후에 모든 것이 어떻게 작동하는지 배우는 데 많은 시간을 보냈습니다. 흩어져있는 정보가 너무 많아서 다음과 같이 한 곳에서 모든 작업을 수행 한 것을 단계별로 공유하기로 결정했습니다.
1) VBA 편집기를 열고 VBA 프로젝트 (YourWorkBookName.xlsm)에서 Microsoft Excel 개체를 열고 변경 이벤트가 포함될 시트를 선택합니다.
2) The default code view is "General." From the drop-down list at the top middle, select "Worksheet."
3) Private Sub Worksheet_SelectionChange is already there as it should be, leave it alone. Copy/Paste Mike Rosenblum's code from above and change the .Range reference to the cell for which you are watching for a change (B3, in my case). Do not place your Macro yet, however (I removed the word "Macro" after "Then"):
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("H5")) Is Nothing Then
End Sub
or from the drop-down list at the top left, select "Change" and in the space between Private Sub and End Sub, paste If Not Intersect(Target, Me.Range("H5")) Is Nothing Then
4) On the line after "Then" turn off events so that when you call your macro, it does not trigger events and try to run this Worksheet_Change again in a never ending cycle that crashes Excel and/or otherwise messes everything up:
Application.EnableEvents = False
5) Call your macro
Call YourMacroName
6) Turn events back on so the next change (and any/all other events) trigger:
Application.EnableEvents = True
7) End the If block and the Sub:
End If
End Sub
The entire code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B3")) Is Nothing Then
Application.EnableEvents = False
Call UpdateAndViewOnly
Application.EnableEvents = True
End If
End Sub
This takes turning events on/off out of the Modules which creates problems and simply lets the change trigger, turns off events, runs your macro and turns events back on.
I have a cell which is linked to online stock database and updated frequently. I want to trigger a macro whenever the cell value is updated.
I believe this is similar to cell value change by a program or any external data update but above examples somehow do not work for me. I think the problem is because excel internal events are not triggered, but thats my guess.
I did the following,
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheets("Symbols").Range("$C$3")) Is Nothing Then
'Run Macro
End Sub
참고URL : https://stackoverflow.com/questions/409434/automatically-execute-an-excel-macro-on-a-cell-change
'program tip' 카테고리의 다른 글
Git : git add -i 또는 git add -e를 사용할 때 더 많은 컨텍스트를 표시합니까? (0) | 2020.09.08 |
---|---|
tempuri.org는 무엇입니까? (0) | 2020.09.08 |
'sudo pip'를 실행하면 어떤 위험이 있습니까? (0) | 2020.09.08 |
은 무슨 뜻인가요? (0) | 2020.09.08 |
Xcode 6에서 IPA를 생성하는 방법은 무엇입니까? (0) | 2020.09.08 |