ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Calling same event for all the toolbox controls in the sheet (https://www.excelbanter.com/excel-programming/355392-calling-same-event-all-toolbox-controls-sheet.html)

neetha

Calling same event for all the toolbox controls in the sheet
 
How can i call the same event for all the toolbox controls in an excel
sheet.
I have got a code from this site to call the same event if the controls
are of same type.
This is the code for calling the keydown event for textboxes. But is
there a way I can use the same code for all the controls in the
sheet(option button, textbox etc).

'in the workbook open

Public C As Collection
Set C = New Collection

For Each Obj In Sheet1.OLEObjects

If (TypeOf Obj.Object Is MSForms.TextBox) Then
C.Add New Class1
With C(C.Count)
Set .TB = Obj.Object
Set .Object = Obj
.Index = C.Count

End With
End If

Next Obj

' in the class1 module

Public WithEvents TB As MSForms.TextBox
Dim I , TBCount As Integer

Private Sub TB_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
If KeyCode < 9 Then Exit Sub
TBCount = ThisWorkbook.C.Count
If Shift Then
If Index = 1 Then I = TBCount Else I = Index - 1
Else
If Index = TBCount Then I = 1 Else I = Index + 1
End If
ActiveWindow.RangeSelection.Select
ThisWorkbook.C(I).Object.Activate
End Sub



All times are GMT +1. The time now is 12:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com