更改多个复选框的事件//Excel VBA//WithEvents//ClassModule
我在一个UserForm上有25个文本框,名称如下更改多个复选框的事件//Excel VBA//WithEvents//ClassModule,excel,vba,event-handling,eventhandler,Excel,Vba,Event Handling,Eventhandler,我在一个UserForm上有25个文本框,名称如下 Name:id[ux]\u box 1=PROJ\u START\u ROW和CInt(ROW\u id)我不确定,但这肯定是一个错误:Me.MyTextBox.BackColor=RGB(255,255,255)No,也许更明显的问题在于BoxesGroup\u更改()> MyTreBox x>更改< /C> >并且在其中没有声明某些变量。当您得到错误时,哪个行被高亮显示?如果您将索引号放在名称的末尾而不是在中间,则您的生活会更容易。然后您可
Name:id[ux]\u box 1=PROJ\u START\u ROW和CInt(ROW\u id)我不确定,但这肯定是一个错误:
Me.MyTextBox.BackColor=RGB(255,255,255)
No,也许更明显的问题在于BoxesGroup\u更改()> MyTreBox x>更改< /C> >并且在其中没有声明某些变量。当您得到错误时,哪个行被高亮显示?如果您将索引号放在名称的末尾而不是在中间,则您的生活会更容易。然后您可以执行类似于lblName=Replace(Me.MyTextBox.Name,“id\u-box”,“desc\u-label”)
Private Sub UserForm_Initialize()
'Code to make single change event subroutine register for all id_[INT]_textboxes on links form
Dim ctrl As MSForms.Control
Dim text_box_handler As text_boxes_change
Set textBox_collection = New Collection
For Each ctrl In Me.controls
If TypeOf ctrl Is MSForms.TextBox Then
If Split(ctrl.Name, "_")(0) = "id" Then
Set text_box_handler = New text_boxes_change
Set text_box_handler.control_text_box = ctrl
textBox_collection.Add text_box_handler
End If
End If
Next ctrl
End Sub
Class Module Name: text_boxes_change
Option Explicit
'This class assists in validating multiple text boxes on forms without having to define event
functions for each text box separately
'Global Constants
Const CASHFLOW As String = "Chart"
Const SETUP As String = "Settings"
Const INVOICE_STATUSES As String = "K13:K18"
Const TIME_UNITS As String = "L21:L24"
Const RELATION_TYPES As String = "M21:M25"
Const ACTIVITIES_COL As String = "T"
Const PROJ_START_ROW As Integer = 6
Public WithEvents MyTextBox As MSForms.TextBox
Public Property Set control_text_box(ByVal tb As MSForms.TextBox)
Set MyTextBox = tb
End Property
Public Sub BoxesGroup_Change()
'Setting default background color for the box
Me.MyTextBox.BackColor = RGB(255, 255, 255)
'Setting up Cashflow Worksheet Object
Dim cashflow_sheet As Worksheet
Set cashflow_sheet = Sheets("Chart")
'Finding lastrow with text inside the Sub-Activites column in Chart sheet
Dim lastrow As Integer
lastrow = cashflow_sheet.Cells(Rows.Count, ACTIVITIES_COL).End(xlUp).Row
'Range to represent the activities column in Chart worksheet
Dim activities_range As Range
Set activities_range = cashflow_sheet.Range(ACTIVITIES_COL & CStr(PROJ_START_ROW) & ":" & _
ACTIVITIES_COL & CStr(lastrow))
'A variable to store the user inputed value for id_box
Dim row_id As String
row_id = Me.MyTextBox.value
If IsNumeric(row_id) = True Then
If CInt(row_id) >= PROJ_START_ROW And CInt(row_id) <= lastrow Then
Dim desc_caption As String
'SheetFunctions is a Module ; links_description is a Function that returns a string
representing a cell address based on the rules of the workbook; functionality is tested and verified
for this part
desc_caption = SheetFunctions.links_description(row_id)
If desc_caption <> "" Then
Me.MyTextBox.BackColor = RGB(255, 255, 255)
Me.desc_1_label.Caption = desc_caption
Else
Me.MyTextBox.BackColor = RGB(140, 39, 30)
End If
Else
Me.MyTextBox.BackColor = RGB(140, 39, 30)
End If
Else
Me.MyTextBox.BackColor = RGB(140, 39, 30)
End If
End Sub