Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kei0425/f345aca2065d68b9f6ceccfb6de4e1fd to your computer and use it in GitHub Desktop.
Save kei0425/f345aca2065d68b9f6ceccfb6de4e1fd to your computer and use it in GitHub Desktop.
Public Class ThisAddIn
Private eventWorksheets As List(Of Excel.Worksheet)
Private Sub ThisAddIn_Startup() Handles Me.Startup
' ワークブックアクティブイベント登録
AddHandler Globals.ThisAddIn.Application.WorkbookActivate, New Excel.AppEvents_WorkbookActivateEventHandler(AddressOf add_handlers_to_book)
End Sub
Private Sub add_handlers_to_book(workbook As Excel.Workbook)
Me.eventWorksheets = New List(Of Excel.Worksheet)
For Each worksheet As Excel.Worksheet In workbook.Worksheets
If worksheet.Name = "Sheet2" Or worksheet.Name = "Sheet3" Then
' シート名が Sheet2かSheet3の場合、イベント登録
AddHandler worksheet.SelectionChange, New Excel.DocEvents_SelectionChangeEventHandler(AddressOf selection_change)
' イベントを設定したワークシートを保持
Me.eventWorksheets.Add(worksheet)
End If
Next
End Sub
Private Sub selection_change(target As Excel.Range)
Dim worksheet As Excel.Worksheet = target.Worksheet
worksheet.Cells(1, 1).Value = target.Address
GC.Collect()
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment